@milaboratories/pl-middle-layer 1.48.17 → 1.48.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/dist/mutator/project.cjs
CHANGED
|
@@ -144,7 +144,14 @@ var ProjectMutator = class ProjectMutator {
|
|
|
144
144
|
}
|
|
145
145
|
fixProblemsAndMigrate() {
|
|
146
146
|
this.blockInfos.forEach((blockInfo) => {
|
|
147
|
-
if (blockInfo.fields.prodArgs === void 0 || blockInfo.fields.prodOutput === void 0 || blockInfo.fields.prodCtx === void 0) this.deleteBlockFields(blockInfo.id, "prodArgs", "prodOutput", "prodCtx");
|
|
147
|
+
if (blockInfo.fields.prodArgs === void 0 || blockInfo.fields.prodOutput === void 0 || blockInfo.fields.prodCtx === void 0) this.deleteBlockFields(blockInfo.id, "prodArgs", "prodOutput", "prodCtx", "prodUiCtx");
|
|
148
|
+
});
|
|
149
|
+
this.blockInfos.forEach((blockInfo) => {
|
|
150
|
+
if (blockInfo.fields.stagingOutput === void 0 || blockInfo.fields.stagingCtx === void 0) this.deleteBlockFields(blockInfo.id, "stagingOutput", "stagingCtx", "stagingUiCtx");
|
|
151
|
+
});
|
|
152
|
+
this.blockInfos.forEach((blockInfo) => {
|
|
153
|
+
if (blockInfo.fields.prodOutputPrevious === void 0 || blockInfo.fields.prodCtxPrevious === void 0) this.deleteBlockFields(blockInfo.id, "prodOutputPrevious", "prodCtxPrevious", "prodUiCtxPrevious");
|
|
154
|
+
if (blockInfo.fields.stagingOutputPrevious === void 0 || blockInfo.fields.stagingCtxPrevious === void 0) this.deleteBlockFields(blockInfo.id, "stagingOutputPrevious", "stagingCtxPrevious", "stagingUiCtxPrevious");
|
|
148
155
|
});
|
|
149
156
|
let initialBlockSettings;
|
|
150
157
|
this.blockInfos.forEach((blockInfo) => {
|
|
@@ -153,6 +160,7 @@ var ProjectMutator = class ProjectMutator {
|
|
|
153
160
|
this.setBlockFieldObj(blockInfo.id, "blockSettings", initialBlockSettings);
|
|
154
161
|
}
|
|
155
162
|
});
|
|
163
|
+
this.blockInfos.forEach((info) => info.check());
|
|
156
164
|
}
|
|
157
165
|
get wasModified() {
|
|
158
166
|
return this.lastModifiedChanged || this.structureChanged || this.fieldsChanged || this.metaChanged || this.renderingStateChanged;
|
|
@@ -419,8 +427,8 @@ var ProjectMutator = class ProjectMutator {
|
|
|
419
427
|
const upstreamContexts = [];
|
|
420
428
|
upstream.forEach((id) => {
|
|
421
429
|
const info = this.getBlockInfo(id);
|
|
422
|
-
if (info.fields["stagingCtx"]?.ref
|
|
423
|
-
|
|
430
|
+
if (info.fields["stagingCtx"]?.ref !== void 0) upstreamContexts.push(_milaboratories_pl_client.Pl.unwrapHolder(this.tx, info.fields["stagingCtx"].ref));
|
|
431
|
+
else if (info.fields.currentPrerunArgs !== void 0) throw new Error(`Upstream ${id} staging is not rendered but has currentPrerunArgs set.`);
|
|
424
432
|
if (info.fields["prodCtx"]?.ref !== void 0) upstreamContexts.push(_milaboratories_pl_client.Pl.unwrapHolder(this.tx, info.fields["prodCtx"].ref));
|
|
425
433
|
});
|
|
426
434
|
return require_render_block.createBContextFromUpstreams(this.tx, upstreamContexts);
|
|
@@ -433,10 +441,10 @@ var ProjectMutator = class ProjectMutator {
|
|
|
433
441
|
* If currentPrerunArgs is not set (prerunArgs returned undefined), skips staging for this block.
|
|
434
442
|
*/
|
|
435
443
|
renderStagingFor(blockId) {
|
|
436
|
-
this.resetStaging(blockId);
|
|
437
444
|
const info = this.getBlockInfo(blockId);
|
|
438
445
|
const prerunArgsRef = info.fields.currentPrerunArgs?.ref;
|
|
439
446
|
if (prerunArgsRef === void 0) return;
|
|
447
|
+
this.resetStaging(blockId);
|
|
440
448
|
const ctx = this.createStagingCtx(this.getStagingGraph().nodes.get(blockId).upstream);
|
|
441
449
|
if (this.getBlock(blockId).renderingMode !== "Heavy") throw new Error("not supported yet");
|
|
442
450
|
const tpl = info.getTemplate(this.tx);
|
|
@@ -883,7 +891,6 @@ var ProjectMutator = class ProjectMutator {
|
|
|
883
891
|
}
|
|
884
892
|
blockInfos.forEach((info) => {
|
|
885
893
|
if (!blockInStruct.has(info.id)) throw new Error(`Inconsistent project structure: no structure entry for ${info.id}`);
|
|
886
|
-
info.check();
|
|
887
894
|
});
|
|
888
895
|
const prj = new ProjectMutator(rid, tx, author, schema, lastModified, meta, structure, renderingState, blocksInLimboSet, blockInfos, ctxExportTplHolder, projectHelper);
|
|
889
896
|
prj.fixProblemsAndMigrate();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"project.cjs","names":["ConsoleLoggerAdapter","Pl","BlockPackTemplateField","InitialBlockSettings","stagingGraph","productionGraph","allBlocks","projectFieldName","BLOCK_STORAGE_FACADE_VERSION","createBContextFromUpstreams","exportContext","createRenderHeavyBlock","createBlockPack","FieldsToDuplicate","graphDiff","Denque","blockArgsAuthorKey","ProjectStructureAuthorKey","ProjectLastModifiedTimestamp","ProjectStructureKey","BlockRenderingStateKey","ProjectMetaKey","SchemaVersionKey","parseProjectField","SchemaVersionCurrent","UiError","getPreparedExportTemplateEnvelope","getServiceTemplateField","loadTemplate","InitialBlockMeta","ProjectResourceType","ProjectCreatedTimestamp","InitialBlockStructure","InitialProjectRenderingState","PlClient","getDebugFlags"],"sources":["../../src/mutator/project.ts"],"sourcesContent":["import type {\n AnyRef,\n AnyResourceRef,\n BasicResourceData,\n PlTransaction,\n ResourceData,\n ResourceId,\n TxOps,\n} from \"@milaboratories/pl-client\";\nimport {\n ensureResourceIdNotNull,\n field,\n isNotNullResourceId,\n isNullResourceId,\n isResource,\n isResourceId,\n isResourceRef,\n Pl,\n PlClient,\n} from \"@milaboratories/pl-client\";\nimport { createRenderHeavyBlock, createBContextFromUpstreams } from \"./template/render_block\";\nimport type {\n Block,\n ProjectStructure,\n ProjectField,\n ProjectRenderingState,\n} from \"../model/project_model\";\nimport {\n BlockRenderingStateKey,\n ProjectStructureKey,\n parseProjectField,\n projectFieldName,\n SchemaVersionCurrent,\n SchemaVersionKey,\n ProjectResourceType,\n InitialBlockStructure,\n InitialProjectRenderingState,\n ProjectMetaKey,\n InitialBlockMeta,\n blockArgsAuthorKey,\n ProjectLastModifiedTimestamp,\n ProjectCreatedTimestamp,\n ProjectStructureAuthorKey,\n getServiceTemplateField,\n FieldsToDuplicate,\n} from \"../model/project_model\";\nimport { BlockPackTemplateField, createBlockPack } from \"./block-pack/block_pack\";\nimport type { BlockGraph, ProductionGraphBlockInfo } from \"../model/project_model_util\";\nimport { allBlocks, graphDiff, productionGraph, stagingGraph } from \"../model/project_model_util\";\nimport type { BlockPackSpecPrepared } from \"../model\";\nimport type {\n AuthorMarker,\n BlockPackSpec,\n BlockSettings,\n ProjectMeta,\n} from \"@milaboratories/pl-model-middle-layer\";\nimport { InitialBlockSettings } from \"@milaboratories/pl-model-middle-layer\";\nimport Denque from \"denque\";\nimport { exportContext, getPreparedExportTemplateEnvelope } from \"./context_export\";\nimport { loadTemplate } from \"./template/template_loading\";\nimport {\n cachedDeserialize,\n notEmpty,\n canonicalJsonBytes,\n cachedDecode,\n type MiLogger,\n ConsoleLoggerAdapter,\n} from \"@milaboratories/ts-helpers\";\nimport type { ProjectHelper } from \"../model/project_helper\";\nimport {\n extractConfig,\n UiError,\n BLOCK_STORAGE_FACADE_VERSION,\n type BlockConfig,\n} from \"@platforma-sdk/model\";\nimport { getDebugFlags } from \"../debug\";\nimport type { BlockPackInfo } from \"../model/block_pack\";\n\ntype FieldStatus = \"NotReady\" | \"Ready\" | \"Error\";\n\ninterface BlockFieldState {\n modCount: number;\n ref?: AnyRef;\n status?: FieldStatus;\n value?: Uint8Array;\n}\n\ntype BlockFieldStates = Partial<Record<ProjectField[\"fieldName\"], BlockFieldState>>;\ntype BlockFieldStateValue = Omit<BlockFieldState, \"modCount\">;\n\ninterface BlockInfoState {\n readonly id: string;\n readonly fields: BlockFieldStates;\n blockConfig?: BlockConfig;\n blockPack?: BlockPackSpec;\n}\n\nfunction cached<ModId, T>(modIdCb: () => ModId, valueCb: () => T): () => T {\n let initialized = false;\n let lastModId: ModId | undefined = undefined;\n let value: T | undefined = undefined;\n return () => {\n if (!initialized) {\n initialized = true;\n lastModId = modIdCb();\n value = valueCb();\n return value;\n }\n const currentModId = modIdCb();\n if (lastModId !== currentModId) {\n lastModId = currentModId;\n value = valueCb();\n }\n return valueCb();\n };\n}\n\nclass BlockInfo {\n constructor(\n public readonly id: string,\n public readonly fields: BlockFieldStates,\n public readonly config: BlockConfig,\n public readonly source: BlockPackSpec,\n private readonly logger: MiLogger = new ConsoleLoggerAdapter(),\n ) {}\n\n public check() {\n // state assertions\n\n if ((this.fields.prodOutput === undefined) !== (this.fields.prodCtx === undefined))\n throw new Error(\"inconsistent prod fields\");\n\n if ((this.fields.stagingOutput === undefined) !== (this.fields.stagingCtx === undefined))\n throw new Error(\"inconsistent stage fields\");\n\n if (\n (this.fields.prodOutputPrevious === undefined) !==\n (this.fields.prodCtxPrevious === undefined)\n )\n throw new Error(\"inconsistent prod cache fields\");\n\n if (\n (this.fields.stagingOutputPrevious === undefined) !==\n (this.fields.stagingCtxPrevious === undefined)\n )\n throw new Error(\"inconsistent stage cache fields\");\n\n if (this.fields.blockPack === undefined) throw new Error(\"no block pack field\");\n\n if (this.fields.blockStorage === undefined) throw new Error(\"no block storage field\");\n }\n\n private readonly currentArgsC = cached(\n () => this.fields.currentArgs?.modCount,\n () => {\n const bin = this.fields.currentArgs?.value;\n if (bin === undefined) return undefined;\n return cachedDeserialize(bin);\n },\n );\n\n private readonly blockStorageC = cached(\n () => this.fields.blockStorage!.modCount,\n () => {\n const bin = this.fields.blockStorage?.value;\n if (bin === undefined) return undefined;\n return cachedDeserialize<Record<string, unknown>>(bin);\n },\n );\n\n private readonly blockStorageJ = cached(\n () => this.fields.blockStorage!.modCount,\n () => {\n const bin = this.fields.blockStorage?.value;\n if (bin === undefined) return undefined;\n return cachedDecode(bin);\n },\n );\n\n private readonly prodArgsC = cached(\n () => this.fields.prodArgs?.modCount,\n () => {\n const bin = this.fields.prodArgs?.value;\n if (bin === undefined) return undefined;\n return cachedDeserialize(bin);\n },\n );\n\n private readonly currentPrerunArgsC = cached(\n () => this.fields.currentPrerunArgs?.modCount,\n () => {\n const bin = this.fields.currentPrerunArgs?.value;\n if (bin === undefined) return undefined;\n return cachedDeserialize(bin);\n },\n );\n\n get currentArgs(): unknown {\n return this.currentArgsC();\n }\n\n get blockStorage(): unknown {\n try {\n return this.blockStorageC();\n } catch (e) {\n this.logger.error(new Error(`Error getting blockStorage for ${this.id}`, { cause: e }));\n return undefined;\n }\n }\n\n get blockStorageJson() {\n return this.blockStorageJ();\n }\n\n get currentPrerunArgs(): unknown {\n return this.currentPrerunArgsC();\n }\n\n get stagingRendered(): boolean {\n return this.fields.stagingCtx !== undefined;\n }\n\n get productionRendered(): boolean {\n return this.fields.prodCtx !== undefined;\n }\n\n get productionHasErrors(): boolean {\n return this.fields.prodUiCtx?.status === \"Error\";\n }\n\n private readonly productionStaleC: () => boolean = cached(\n () => `${this.fields.currentArgs!.modCount}_${this.fields.prodArgs?.modCount}`,\n () =>\n this.fields.prodArgs === undefined ||\n Buffer.compare(this.fields.currentArgs!.value!, this.fields.prodArgs.value!) !== 0,\n );\n\n get requireProductionRendering(): boolean {\n return !this.productionRendered || this.productionStaleC() || this.productionHasErrors;\n }\n\n /** Returns true if staging should be re-rendered (stagingCtx is not set) */\n get requireStagingRendering(): boolean {\n // No staging needed if currentPrerunArgs is undefined (args derivation failed)\n if (this.fields.currentPrerunArgs === undefined) return false;\n return !this.stagingRendered;\n }\n\n get prodArgs(): unknown {\n return this.prodArgsC();\n }\n\n public getTemplate(tx: PlTransaction): AnyRef {\n return tx.getFutureFieldValue(\n Pl.unwrapHolder(tx, this.fields.blockPack!.ref!),\n BlockPackTemplateField,\n \"Input\",\n );\n }\n}\n\n/**\n * Specification for creating a new block.\n * Discriminated union based on `storageMode`.\n */\n/** Specification for creating a new block. Discriminated union based on `storageMode`. */\nexport type NewBlockSpec =\n | { storageMode: \"fromModel\"; blockPack: BlockPackSpecPrepared }\n | { storageMode: \"legacy\"; blockPack: BlockPackSpecPrepared; legacyState: string };\n\nconst NoNewBlocks = (blockId: string) => {\n throw new Error(`No new block info for ${blockId}`);\n};\n\n/**\n * Request to set block state using unified state format.\n * For v3 blocks: state is the block's state\n * For v1/v2 blocks: state should be { args, uiState } format\n */\nexport type SetStatesRequest =\n | {\n blockId: string;\n /** The unified state to set */\n state: unknown;\n modelAPIVersion: 1;\n }\n | {\n blockId: string;\n /** Storage operation payload - middle layer is agnostic to specific operations */\n payload: { operation: string; value: unknown };\n modelAPIVersion: 2;\n };\n\nexport type ClearState = {\n state: unknown;\n};\n\nexport class ProjectMutator {\n private globalModCount = 0;\n private fieldsChanged: boolean = false;\n\n //\n // Change trackers\n //\n\n private lastModifiedChanged = false;\n private structureChanged = false;\n private metaChanged = false;\n private renderingStateChanged = false;\n\n /** Set blocks will be assigned current mutator author marker on save */\n private readonly blocksWithChangedInputs = new Set<string>();\n\n constructor(\n public readonly rid: ResourceId,\n private readonly tx: PlTransaction,\n private readonly author: AuthorMarker | undefined,\n private readonly schema: string,\n private lastModified: number,\n private meta: ProjectMeta,\n private struct: ProjectStructure,\n private readonly renderingState: Omit<ProjectRenderingState, \"blocksInLimbo\">,\n private readonly blocksInLimbo: Set<string>,\n private readonly blockInfos: Map<string, BlockInfo>,\n private readonly ctxExportTplHolder: AnyResourceRef,\n private readonly projectHelper: ProjectHelper,\n ) {}\n\n private fixProblemsAndMigrate() {\n // Fixing problems introduced by old code\n this.blockInfos.forEach((blockInfo) => {\n if (\n blockInfo.fields.prodArgs === undefined ||\n blockInfo.fields.prodOutput === undefined ||\n blockInfo.fields.prodCtx === undefined\n )\n this.deleteBlockFields(blockInfo.id, \"prodArgs\", \"prodOutput\", \"prodCtx\");\n });\n\n // Migration for addition of block settings field\n let initialBlockSettings: Omit<BlockFieldState, \"modCount\"> | undefined;\n this.blockInfos.forEach((blockInfo) => {\n if (blockInfo.fields.blockSettings === undefined) {\n if (initialBlockSettings === undefined)\n initialBlockSettings = this.createJsonFieldValue(InitialBlockSettings);\n this.setBlockFieldObj(blockInfo.id, \"blockSettings\", initialBlockSettings);\n }\n });\n }\n\n get wasModified(): boolean {\n return (\n this.lastModifiedChanged ||\n this.structureChanged ||\n this.fieldsChanged ||\n this.metaChanged ||\n this.renderingStateChanged\n );\n }\n\n get structure(): ProjectStructure {\n return JSON.parse(JSON.stringify(this.struct)) as ProjectStructure;\n }\n\n //\n // Graph calculation\n //\n\n private stagingGraph: BlockGraph | undefined = undefined;\n private pendingProductionGraph: BlockGraph | undefined = undefined;\n private actualProductionGraph: BlockGraph | undefined = undefined;\n\n private getStagingGraph(): BlockGraph {\n if (this.stagingGraph === undefined) this.stagingGraph = stagingGraph(this.struct);\n return this.stagingGraph;\n }\n\n private getProductionGraphBlockInfo(\n blockId: string,\n prod: boolean,\n ): ProductionGraphBlockInfo | undefined {\n const bInfo = this.getBlockInfo(blockId);\n\n let argsField: BlockFieldState | undefined;\n let args: unknown;\n\n if (prod) {\n if (bInfo.fields.prodArgs === undefined) return undefined;\n argsField = bInfo.fields.prodArgs;\n args = bInfo.prodArgs;\n } else {\n argsField = bInfo.fields.currentArgs;\n args = bInfo.currentArgs;\n }\n\n // Can't compute enrichment targets without args\n if (argsField === undefined) {\n return { args, enrichmentTargets: undefined };\n }\n\n const blockPackField = notEmpty(bInfo.fields.blockPack);\n\n if (isResourceId(argsField.ref!) && isResourceId(blockPackField.ref!))\n return {\n args,\n enrichmentTargets: this.projectHelper.getEnrichmentTargets(\n () => bInfo.config,\n () => args,\n { argsRid: argsField.ref, blockPackRid: blockPackField.ref },\n ),\n };\n else\n return {\n args,\n enrichmentTargets: this.projectHelper.getEnrichmentTargets(\n () => bInfo.config,\n () => args,\n ),\n };\n }\n\n private getPendingProductionGraph(): BlockGraph {\n if (this.pendingProductionGraph === undefined)\n this.pendingProductionGraph = productionGraph(this.struct, (blockId) =>\n this.getProductionGraphBlockInfo(blockId, false),\n );\n return this.pendingProductionGraph;\n }\n\n private getActualProductionGraph(): BlockGraph {\n if (this.actualProductionGraph === undefined)\n this.actualProductionGraph = productionGraph(this.struct, (blockId) =>\n this.getProductionGraphBlockInfo(blockId, true),\n );\n return this.actualProductionGraph;\n }\n\n //\n // Generic helpers to interact with project state\n //\n\n private getBlockInfo(blockId: string): BlockInfo {\n const info = this.blockInfos.get(blockId);\n if (info === undefined) throw new Error(`No such block: ${blockId}`);\n return info;\n }\n\n private createJsonFieldValueByContent(content: string): BlockFieldStateValue {\n if (content === undefined) throw new Error(\"content is undefined\");\n const value = Buffer.from(content);\n const ref = this.tx.createValue(Pl.JsonObject, value);\n return { ref, value, status: \"Ready\" };\n }\n\n private createJsonFieldValue(obj: unknown): BlockFieldStateValue {\n return this.createJsonFieldValueByContent(JSON.stringify(obj));\n }\n\n private getBlock(blockId: string): Block {\n for (const block of allBlocks(this.struct)) if (block.id === blockId) return block;\n throw new Error(\"block not found\");\n }\n\n private setBlockFieldObj(\n blockId: string,\n fieldName: keyof BlockFieldStates,\n state: BlockFieldStateValue,\n ) {\n const fid = field(this.rid, projectFieldName(blockId, fieldName));\n\n if (state.ref === undefined) throw new Error(\"Can't set value with empty ref\");\n\n if (this.getBlockInfo(blockId).fields[fieldName] === undefined)\n this.tx.createField(fid, \"Dynamic\", state.ref);\n else this.tx.setField(fid, state.ref);\n\n this.getBlockInfo(blockId).fields[fieldName] = {\n modCount: this.globalModCount++,\n ...state,\n };\n\n this.fieldsChanged = true;\n }\n\n private setBlockField(\n blockId: string,\n fieldName: keyof BlockFieldStates,\n ref: AnyRef,\n status: FieldStatus,\n value?: Uint8Array,\n ) {\n this.setBlockFieldObj(blockId, fieldName, { ref, status, value });\n }\n\n private deleteBlockFields(blockId: string, ...fieldNames: (keyof BlockFieldStates)[]): boolean {\n let deleted = false;\n const info = this.getBlockInfo(blockId);\n for (const fieldName of fieldNames) {\n const fields = info.fields;\n if (!(fieldName in fields)) continue;\n this.tx.removeField(field(this.rid, projectFieldName(blockId, fieldName)));\n delete fields[fieldName];\n this.fieldsChanged = true;\n deleted = true;\n }\n return deleted;\n }\n\n private updateLastModified() {\n this.lastModified = Date.now();\n this.lastModifiedChanged = true;\n }\n\n //\n // Main project actions\n //\n\n private resetStagingRefreshTimestamp() {\n this.renderingState.stagingRefreshTimestamp = Date.now();\n this.renderingStateChanged = true;\n }\n\n private resetStaging(blockId: string): void {\n const fields = this.getBlockInfo(blockId).fields;\n if (\n fields.stagingOutput?.status === \"Ready\" &&\n fields.stagingCtx?.status === \"Ready\" &&\n fields.stagingUiCtx?.status === \"Ready\"\n ) {\n this.setBlockFieldObj(blockId, \"stagingOutputPrevious\", fields.stagingOutput);\n this.setBlockFieldObj(blockId, \"stagingCtxPrevious\", fields.stagingCtx);\n this.setBlockFieldObj(blockId, \"stagingUiCtxPrevious\", fields.stagingUiCtx);\n }\n if (this.deleteBlockFields(blockId, \"stagingOutput\", \"stagingCtx\", \"stagingUiCtx\"))\n this.resetStagingRefreshTimestamp();\n }\n\n private resetProduction(blockId: string): void {\n const fields = this.getBlockInfo(blockId).fields;\n if (\n fields.prodOutput?.status === \"Ready\" &&\n fields.prodCtx?.status === \"Ready\" &&\n fields.prodUiCtx?.status === \"Ready\"\n ) {\n this.setBlockFieldObj(blockId, \"prodOutputPrevious\", fields.prodOutput);\n this.setBlockFieldObj(blockId, \"prodCtxPrevious\", fields.prodCtx);\n this.setBlockFieldObj(blockId, \"prodUiCtxPrevious\", fields.prodUiCtx);\n }\n this.deleteBlockFields(blockId, \"prodOutput\", \"prodCtx\", \"prodUiCtx\", \"prodArgs\");\n }\n\n /** Running blocks are reset, already computed moved to limbo. Returns if\n * either of the actions were actually performed.\n * This method ensures the block is left in a consistent state that passes check() constraints. */\n private resetOrLimboProduction(blockId: string): boolean {\n const fields = this.getBlockInfo(blockId).fields;\n\n // Check if we can safely move to limbo (both core production fields are ready)\n if (fields.prodOutput?.status === \"Ready\" && fields.prodCtx?.status === \"Ready\") {\n if (this.blocksInLimbo.has(blockId))\n // we are already in limbo\n return false;\n\n // limbo - keep the ready production results but clean up cache\n this.blocksInLimbo.add(blockId);\n this.renderingStateChanged = true;\n\n // doing some gc - clean up previous cache fields\n this.deleteBlockFields(blockId, \"prodOutputPrevious\", \"prodCtxPrevious\", \"prodUiCtxPrevious\");\n\n return true;\n } else {\n // reset - clean up any partial/inconsistent production stat\n return this.deleteBlockFields(\n blockId,\n \"prodOutput\",\n \"prodCtx\",\n \"prodUiCtx\",\n \"prodArgs\",\n \"prodOutputPrevious\",\n \"prodCtxPrevious\",\n \"prodUiCtxPrevious\",\n );\n }\n }\n\n /**\n * Gets current block state and merges with partial updates.\n * Used by legacy v1/v2 methods like setBlockArgs and setUiState.\n *\n * @param blockId The block to get state for\n * @param partialUpdate Partial state to merge (e.g. { args } or { uiState })\n * @returns Merged state in unified format { args, uiState }\n */\n public mergeBlockState(\n blockId: string,\n partialUpdate: { args?: unknown; uiState?: unknown },\n ): { args?: unknown; uiState?: unknown } {\n const info = this.getBlockInfo(blockId);\n const currentState = info.blockStorage as { args?: unknown; uiState?: unknown } | undefined;\n if (currentState === undefined) {\n throw new Error(`Cannot merge block state for ${blockId}: blockStorage is unavailable`);\n }\n return { ...currentState, ...partialUpdate };\n }\n\n /**\n * Sets raw block storage content directly (for testing purposes).\n * This bypasses all normalization and VM transformations.\n *\n * @param blockId The block to set storage for\n * @param rawStorageJson Raw storage as JSON string\n */\n public setBlockStorageRaw(blockId: string, rawStorageJson: string): void {\n this.setBlockFieldObj(\n blockId,\n \"blockStorage\",\n this.createJsonFieldValueByContent(rawStorageJson),\n );\n this.blocksWithChangedInputs.add(blockId);\n this.updateLastModified();\n }\n\n /**\n * Resets a v2+ block to its initial storage state.\n * Gets initial storage from VM and derives args from it.\n *\n * For v1 blocks, use setStates() instead.\n *\n * @param blockId The block to reset\n */\n public resetToInitialStorage(blockId: string): void {\n const info = this.getBlockInfo(blockId);\n const blockConfig = info.config;\n\n if (blockConfig.modelAPIVersion !== BLOCK_STORAGE_FACADE_VERSION) {\n throw new Error(\"resetToInitialStorage is only supported for model API version 2\");\n }\n\n // Get initial storage from VM\n const initialStorageJson = this.projectHelper.getInitialStorageInVM(blockConfig);\n this.setBlockStorageRaw(blockId, initialStorageJson);\n\n // Derive args from storage - set or clear currentArgs based on derivation result\n const deriveArgsResult = this.projectHelper.deriveArgsFromStorage(\n blockConfig,\n initialStorageJson,\n );\n if (!deriveArgsResult.error) {\n this.setBlockFieldObj(\n blockId,\n \"currentArgs\",\n this.createJsonFieldValue(deriveArgsResult.value),\n );\n // Derive prerunArgs from storage\n const prerunArgs = this.projectHelper.derivePrerunArgsFromStorage(\n blockConfig,\n initialStorageJson,\n );\n if (prerunArgs !== undefined) {\n this.setBlockFieldObj(blockId, \"currentPrerunArgs\", this.createJsonFieldValue(prerunArgs));\n } else {\n this.deleteBlockFields(blockId, \"currentPrerunArgs\");\n }\n } else {\n if (info.fields.currentPrerunArgs !== undefined) {\n this.projectHelper.logger.warn(\n `[staging] ${blockId}: currentPrerunArgs cleared (args derivation failed)`,\n );\n }\n this.deleteBlockFields(blockId, \"currentArgs\");\n this.deleteBlockFields(blockId, \"currentPrerunArgs\");\n }\n }\n\n /** Optimally sets inputs for multiple blocks in one go */\n public setStates(requests: SetStatesRequest[]) {\n const changedArgs: string[] = [];\n let somethingChanged = false;\n for (const req of requests) {\n const info = this.getBlockInfo(req.blockId);\n let blockChanged = false;\n\n const blockConfig = info.config;\n // modelAPIVersion === 2 means BlockModelV3 with .args() lambda for deriving args\n\n if (req.modelAPIVersion !== blockConfig.modelAPIVersion) {\n throw new Error(\n `Model API version mismatch for block ${req.blockId}: ${req.modelAPIVersion} !== ${blockConfig.modelAPIVersion}`,\n );\n }\n\n // Derive args from storage using the block's config.args() callback\n let args: unknown;\n let prerunArgs: unknown;\n\n if (req.modelAPIVersion === BLOCK_STORAGE_FACADE_VERSION) {\n const currentStorageJson = info.blockStorageJson;\n if (currentStorageJson === undefined) {\n throw new Error(`Block ${req.blockId} has no blockStorage - this should not happen`);\n }\n\n // Apply the state update to storage\n const updatedStorageJson = this.projectHelper.applyStorageUpdateInVM(\n blockConfig,\n currentStorageJson,\n req.payload,\n );\n\n this.setBlockFieldObj(\n req.blockId,\n \"blockStorage\",\n this.createJsonFieldValueByContent(updatedStorageJson),\n );\n\n // Derive args directly from storage (VM extracts data internally)\n const derivedArgsResult = this.projectHelper.deriveArgsFromStorage(\n blockConfig,\n updatedStorageJson,\n );\n if (derivedArgsResult.error) {\n args = undefined;\n prerunArgs = undefined;\n } else {\n args = derivedArgsResult.value;\n // Derive prerunArgs from storage, or fall back to args\n prerunArgs = this.projectHelper.derivePrerunArgsFromStorage(\n blockConfig,\n updatedStorageJson,\n );\n }\n } else {\n this.setBlockFieldObj(req.blockId, \"blockStorage\", this.createJsonFieldValue(req.state));\n if (req.state !== null && typeof req.state === \"object\" && \"args\" in req.state) {\n args = (req.state as { args: unknown }).args;\n } else {\n args = req.state;\n }\n // For the legacy blocks, prerunArgs = args (same as production args)\n prerunArgs = args;\n }\n\n // Set or clear currentArgs based on derivation result\n if (args !== undefined) {\n const currentArgsData = canonicalJsonBytes(args);\n const argsPartRef = this.tx.createValue(Pl.JsonObject, currentArgsData);\n this.setBlockField(req.blockId, \"currentArgs\", argsPartRef, \"Ready\", currentArgsData);\n } else {\n this.deleteBlockFields(req.blockId, \"currentArgs\");\n }\n\n // Set currentPrerunArgs field and check if it actually changed\n let prerunArgsChanged = false;\n if (prerunArgs !== undefined) {\n const prerunArgsData = canonicalJsonBytes(prerunArgs);\n const oldPrerunArgsData = info.fields.currentPrerunArgs?.value;\n // Check if prerunArgs actually changed\n if (\n oldPrerunArgsData === undefined ||\n Buffer.compare(oldPrerunArgsData, prerunArgsData) !== 0\n ) {\n prerunArgsChanged = true;\n }\n const prerunArgsRef = this.tx.createValue(Pl.JsonObject, prerunArgsData);\n this.setBlockField(\n req.blockId,\n \"currentPrerunArgs\",\n prerunArgsRef,\n \"Ready\",\n prerunArgsData,\n );\n } else {\n this.deleteBlockFields(req.blockId, \"currentPrerunArgs\");\n }\n\n blockChanged = true;\n // Only add to changedArgs if prerunArgs changed - this controls staging reset\n if (prerunArgsChanged) {\n changedArgs.push(req.blockId);\n }\n\n if (blockChanged) {\n // will be assigned our author marker\n this.blocksWithChangedInputs.add(req.blockId);\n somethingChanged = true;\n }\n }\n\n // resetting staging outputs for all downstream blocks\n this.getStagingGraph().traverse(\"downstream\", changedArgs, ({ id }) => this.resetStaging(id));\n\n if (somethingChanged) this.updateLastModified();\n }\n\n public setBlockSettings(blockId: string, newValue: BlockSettings): void {\n this.setBlockFieldObj(blockId, \"blockSettings\", this.createJsonFieldValue(newValue));\n this.updateLastModified();\n }\n\n private createProdCtx(upstream: Set<string>): AnyRef {\n const upstreamContexts: AnyRef[] = [];\n upstream.forEach((id) => {\n const info = this.getBlockInfo(id);\n if (info.fields[\"prodCtx\"]?.ref === undefined)\n throw new Error(\"One of the upstreams staging is not rendered.\");\n upstreamContexts.push(Pl.unwrapHolder(this.tx, info.fields[\"prodCtx\"].ref));\n });\n return createBContextFromUpstreams(this.tx, upstreamContexts);\n }\n\n private createStagingCtx(upstream: Set<string>): AnyRef {\n const upstreamContexts: AnyRef[] = [];\n upstream.forEach((id) => {\n const info = this.getBlockInfo(id);\n if (info.fields[\"stagingCtx\"]?.ref === undefined)\n throw new Error(\"One of the upstreams staging is not rendered.\");\n upstreamContexts.push(Pl.unwrapHolder(this.tx, info.fields[\"stagingCtx\"].ref));\n if (info.fields[\"prodCtx\"]?.ref !== undefined)\n upstreamContexts.push(Pl.unwrapHolder(this.tx, info.fields[\"prodCtx\"].ref));\n });\n return createBContextFromUpstreams(this.tx, upstreamContexts);\n }\n\n private exportCtx(ctx: AnyRef): AnyRef {\n return exportContext(this.tx, Pl.unwrapHolder(this.tx, this.ctxExportTplHolder), ctx);\n }\n\n /**\n * Renders staging for a block using currentPrerunArgs.\n * If currentPrerunArgs is not set (prerunArgs returned undefined), skips staging for this block.\n */\n private renderStagingFor(blockId: string) {\n this.resetStaging(blockId);\n\n const info = this.getBlockInfo(blockId);\n\n // If currentPrerunArgs is not set (prerunArgs returned undefined), skip staging for this block\n const prerunArgsRef = info.fields.currentPrerunArgs?.ref;\n if (prerunArgsRef === undefined) {\n return;\n }\n\n const ctx = this.createStagingCtx(this.getStagingGraph().nodes.get(blockId)!.upstream);\n\n if (this.getBlock(blockId).renderingMode !== \"Heavy\") throw new Error(\"not supported yet\");\n\n const tpl = info.getTemplate(this.tx);\n\n // Use currentPrerunArgs for staging rendering\n const results = createRenderHeavyBlock(this.tx, tpl, {\n args: prerunArgsRef,\n blockId: this.tx.createValue(Pl.JsonString, JSON.stringify(blockId)),\n isProduction: this.tx.createValue(Pl.JsonBool, JSON.stringify(false)),\n context: ctx,\n });\n\n // Here we set the staging ctx to the input context of the staging workflow, not the output because exports\n // of one staging context should stay within the same block, and not travel downstream.\n // We may change this decision in the future if wanted to support traveling staging exports downstream.\n this.setBlockField(blockId, \"stagingCtx\", Pl.wrapInEphHolder(this.tx, ctx), \"NotReady\");\n\n // Yet the staging UI Ctx is the output context of the staging workflow, because it is used to form the result pool\n // thus creating a certain discrepancy between staging workflow context behavior and desktop's result pool.\n this.setBlockField(blockId, \"stagingUiCtx\", this.exportCtx(results.context), \"NotReady\");\n this.setBlockField(blockId, \"stagingOutput\", results.result, \"NotReady\");\n }\n\n private renderProductionFor(blockId: string) {\n this.resetProduction(blockId);\n\n const info = this.getBlockInfo(blockId);\n\n // Can't render production if currentArgs is not set\n if (info.fields.currentArgs === undefined) {\n throw new Error(`Can't render production for block ${blockId}: currentArgs not set`);\n }\n\n const ctx = this.createProdCtx(this.getPendingProductionGraph().nodes.get(blockId)!.upstream);\n\n if (this.getBlock(blockId).renderingMode === \"Light\")\n throw new Error(\"Can't render production for light block.\");\n\n const tpl = info.getTemplate(this.tx);\n\n const results = createRenderHeavyBlock(this.tx, tpl, {\n args: info.fields.currentArgs.ref!,\n blockId: this.tx.createValue(Pl.JsonString, JSON.stringify(blockId)),\n isProduction: this.tx.createValue(Pl.JsonBool, JSON.stringify(true)),\n context: ctx,\n });\n this.setBlockField(\n blockId,\n \"prodCtx\",\n Pl.wrapInEphHolder(this.tx, results.context),\n \"NotReady\",\n );\n this.setBlockField(blockId, \"prodUiCtx\", this.exportCtx(results.context), \"NotReady\");\n this.setBlockField(blockId, \"prodOutput\", results.result, \"NotReady\");\n\n // saving inputs for which we rendered the production\n this.setBlockFieldObj(blockId, \"prodArgs\", info.fields.currentArgs);\n\n // removing block from limbo as we juts rendered fresh production for it\n if (this.blocksInLimbo.delete(blockId)) this.renderingStateChanged = true;\n }\n\n //\n // Structure changes\n //\n\n private initializeNewBlock(blockId: string, spec: NewBlockSpec): void {\n const info = new BlockInfo(\n blockId,\n {},\n extractConfig(spec.blockPack.config),\n spec.blockPack.source,\n this.projectHelper.logger,\n );\n this.blockInfos.set(blockId, info);\n\n // block pack\n const bp = createBlockPack(this.tx, spec.blockPack);\n this.setBlockField(blockId, \"blockPack\", Pl.wrapInHolder(this.tx, bp), \"NotReady\");\n\n // settings\n this.setBlockFieldObj(\n blockId,\n \"blockSettings\",\n this.createJsonFieldValue(InitialBlockSettings),\n );\n\n const blockConfig = info.config;\n\n let args: unknown;\n let prerunArgs: unknown;\n let storageToWrite: string;\n\n if (spec.storageMode === \"fromModel\") {\n // Model API v2+: get initial storage and derive args from it\n storageToWrite = this.projectHelper.getInitialStorageInVM(blockConfig);\n\n // Derive args directly from storage (VM extracts data internally)\n const deriveArgsResult = this.projectHelper.deriveArgsFromStorage(\n blockConfig,\n storageToWrite,\n );\n if (deriveArgsResult.error) {\n args = undefined;\n prerunArgs = undefined;\n } else {\n args = deriveArgsResult.value;\n prerunArgs = this.projectHelper.derivePrerunArgsFromStorage(blockConfig, storageToWrite);\n }\n } else if (spec.storageMode === \"legacy\") {\n // Model API v1: use legacyState from spec\n const parsedState = JSON.parse(spec.legacyState);\n args = parsedState.args;\n if (args === undefined) {\n throw new Error(\"args is undefined in legacyState\");\n }\n prerunArgs = args;\n storageToWrite = spec.legacyState;\n } else {\n throw new Error(`Unknown storageMode: ${(spec as NewBlockSpec).storageMode}`);\n }\n\n // currentArgs\n if (args !== undefined) {\n this.setBlockFieldObj(blockId, \"currentArgs\", this.createJsonFieldValue(args));\n }\n\n // currentPrerunArgs\n if (prerunArgs !== undefined) {\n this.setBlockFieldObj(blockId, \"currentPrerunArgs\", this.createJsonFieldValue(prerunArgs));\n }\n\n // blockStorage\n this.setBlockFieldObj(\n blockId,\n \"blockStorage\",\n this.createJsonFieldValueByContent(storageToWrite),\n );\n\n // checking structure\n info.check();\n }\n\n private getFieldNamesToDuplicate(blockId: string): Set<ProjectField[\"fieldName\"]> {\n const fields = this.getBlockInfo(blockId).fields;\n\n const diff = <T>(setA: Set<T>, setB: Set<T>): Set<T> =>\n new Set([...setA].filter((x) => !setB.has(x)));\n\n // Check if we can safely move to limbo (both core production fields are ready)\n if (fields.prodOutput?.status === \"Ready\" && fields.prodCtx?.status === \"Ready\") {\n if (this.blocksInLimbo.has(blockId))\n // we are already in limbo\n return FieldsToDuplicate;\n\n return diff(\n FieldsToDuplicate,\n new Set([\"prodOutputPrevious\", \"prodCtxPrevious\", \"prodUiCtxPrevious\"]),\n );\n } else {\n return diff(\n FieldsToDuplicate,\n new Set([\n \"prodOutput\",\n \"prodCtx\",\n \"prodUiCtx\",\n \"prodArgs\",\n \"prodOutputPrevious\",\n \"prodCtxPrevious\",\n \"prodUiCtxPrevious\",\n ]),\n );\n }\n }\n\n private initializeBlockDuplicate(blockId: string, originalBlockInfo: BlockInfo) {\n const info = new BlockInfo(\n blockId,\n {},\n originalBlockInfo.config,\n originalBlockInfo.source,\n this.projectHelper.logger,\n );\n\n this.blockInfos.set(blockId, info);\n\n const fieldNamesToDuplicate = this.getFieldNamesToDuplicate(blockId);\n\n // Copy all fields from original block to new block by sharing references\n for (const [fieldName, fieldState] of Object.entries(originalBlockInfo.fields)) {\n if (\n fieldNamesToDuplicate.has(fieldName as ProjectField[\"fieldName\"]) &&\n fieldState &&\n fieldState.ref\n ) {\n this.setBlockFieldObj(blockId, fieldName as keyof BlockFieldStates, {\n ref: fieldState.ref,\n status: fieldState.status,\n value: fieldState.value,\n });\n }\n }\n\n this.resetOrLimboProduction(blockId);\n\n info.check();\n }\n\n /** Very generic method, better check for more specialized case-specific methods first. */\n public updateStructure(\n newStructure: ProjectStructure,\n newBlockInitializer: (blockId: string) => void = NoNewBlocks,\n ): void {\n const currentStagingGraph = this.getStagingGraph();\n const currentActualProductionGraph = this.getActualProductionGraph();\n\n const newStagingGraph = stagingGraph(newStructure);\n\n const stagingDiff = graphDiff(currentStagingGraph, newStagingGraph);\n\n // removing blocks\n for (const blockId of stagingDiff.onlyInA) {\n const { fields } = this.getBlockInfo(blockId);\n this.deleteBlockFields(blockId, ...(Object.keys(fields) as ProjectField[\"fieldName\"][]));\n this.blockInfos.delete(blockId);\n if (this.blocksInLimbo.delete(blockId)) this.renderingStateChanged = true;\n }\n\n // creating new blocks\n for (const blockId of stagingDiff.onlyInB) {\n newBlockInitializer(blockId);\n }\n\n // resetting stagings affected by topology change\n for (const blockId of stagingDiff.different) this.resetStaging(blockId);\n\n // new actual production graph without new blocks\n const newActualProductionGraph = productionGraph(newStructure, (blockId) =>\n this.getProductionGraphBlockInfo(blockId, true),\n );\n\n const prodDiff = graphDiff(currentActualProductionGraph, newActualProductionGraph);\n\n // applying changes due to topology change in production to affected nodes and\n // all their downstreams\n currentActualProductionGraph.traverse(\"downstream\", [...prodDiff.different], (node) => {\n this.resetOrLimboProduction(node.id);\n });\n\n if (\n stagingDiff.onlyInB.size > 0 ||\n stagingDiff.onlyInA.size > 0 ||\n stagingDiff.different.size > 0\n )\n this.resetStagingRefreshTimestamp();\n\n this.struct = newStructure;\n this.structureChanged = true;\n this.stagingGraph = undefined;\n this.pendingProductionGraph = undefined;\n this.actualProductionGraph = undefined;\n\n this.updateLastModified();\n }\n\n //\n // Structure change helpers\n //\n\n public addBlock(block: Block, spec: NewBlockSpec, before?: string): void {\n const newStruct = this.structure; // copy current structure\n if (before === undefined) {\n // adding as a very last block\n newStruct.groups[newStruct.groups.length - 1].blocks.push(block);\n } else {\n let done = false;\n for (const group of newStruct.groups) {\n const idx = group.blocks.findIndex((b) => b.id === before);\n if (idx < 0) continue;\n group.blocks.splice(idx, 0, block);\n done = true;\n break;\n }\n if (!done) throw new Error(`Can't find element with id: ${before}`);\n }\n this.updateStructure(newStruct, (blockId) => {\n if (blockId !== block.id) throw new Error(\"Unexpected\");\n this.initializeNewBlock(blockId, spec);\n });\n }\n\n /**\n * Duplicates an existing block by copying all its fields and structure.\n * This method creates a deep copy of the block at the mutator level.\n *\n * @param originalBlockId id of the block to duplicate\n * @param newBlockId id for the new duplicated block\n * @param after id of the block to insert new block after\n */\n public duplicateBlock(originalBlockId: string, newBlockId: string, after?: string): void {\n // Get the original block from structure\n const originalBlock = this.getBlock(originalBlockId);\n const originalBlockInfo = this.getBlockInfo(originalBlockId);\n\n // Create new block in structure\n const newBlock: Block = {\n id: newBlockId,\n label: originalBlock.label,\n renderingMode: originalBlock.renderingMode,\n };\n\n // Add the new block to structure\n const newStruct = this.structure; // copy current structure\n if (after === undefined) {\n // adding as a very last block\n newStruct.groups[newStruct.groups.length - 1].blocks.push(newBlock);\n } else {\n let done = false;\n for (const group of newStruct.groups) {\n const idx = group.blocks.findIndex((b) => b.id === after);\n if (idx < 0) continue;\n group.blocks.splice(idx + 1, 0, newBlock);\n done = true;\n break;\n }\n if (!done) throw new Error(`Can't find element with id: ${after}`);\n }\n\n this.updateStructure(newStruct, (blockId) => {\n if (blockId !== newBlockId) throw new Error(\"Unexpected\");\n this.initializeBlockDuplicate(blockId, originalBlockInfo);\n });\n }\n\n public deleteBlock(blockId: string): void {\n const newStruct = this.structure; // copy current structure\n let done = false;\n for (const group of newStruct.groups) {\n const idx = group.blocks.findIndex((b) => b.id === blockId);\n if (idx < 0) continue;\n group.blocks.splice(idx, 1);\n done = true;\n break;\n }\n if (!done) throw new Error(`Can't find element with id: ${blockId}`);\n this.updateStructure(newStruct);\n }\n\n //\n // Block-pack migration\n //\n\n public migrateBlockPack(\n blockId: string,\n spec: BlockPackSpecPrepared,\n newClearState?: ClearState,\n ): void {\n const info = this.getBlockInfo(blockId);\n const newConfig = extractConfig(spec.config);\n\n const persistBlockPack = () => {\n this.setBlockField(\n blockId,\n \"blockPack\",\n Pl.wrapInHolder(this.tx, createBlockPack(this.tx, spec)),\n \"NotReady\",\n );\n };\n\n const applyStorageAndDeriveArgs = (storageJson: string) => {\n persistBlockPack();\n this.setBlockStorageRaw(blockId, storageJson);\n const deriveArgsResult = this.projectHelper.deriveArgsFromStorage(newConfig, storageJson);\n if (!deriveArgsResult.error) {\n this.setBlockFieldObj(\n blockId,\n \"currentArgs\",\n this.createJsonFieldValue(deriveArgsResult.value),\n );\n const prerunArgs = this.projectHelper.derivePrerunArgsFromStorage(newConfig, storageJson);\n if (prerunArgs !== undefined) {\n this.setBlockFieldObj(\n blockId,\n \"currentPrerunArgs\",\n this.createJsonFieldValue(prerunArgs),\n );\n } else {\n this.deleteBlockFields(blockId, \"currentPrerunArgs\");\n }\n } else {\n this.deleteBlockFields(blockId, \"currentArgs\");\n this.deleteBlockFields(blockId, \"currentPrerunArgs\");\n }\n };\n\n if (newClearState !== undefined) {\n // State is being reset - no migration needed\n const supportsStorageFromVM = newConfig.modelAPIVersion === BLOCK_STORAGE_FACADE_VERSION;\n\n if (supportsStorageFromVM) {\n // V2+: Get initial storage directly from VM and derive args from it\n const initialStorageJson = this.projectHelper.getInitialStorageInVM(newConfig);\n applyStorageAndDeriveArgs(initialStorageJson);\n this.blocksWithChangedInputs.add(blockId);\n this.updateLastModified();\n } else {\n // V1: Use setStates with legacy state format\n persistBlockPack();\n this.setStates([{ modelAPIVersion: 1, blockId, state: newClearState.state }]);\n }\n } else {\n // State is being preserved - run migrations if needed via VM\n // Only Model API v2 blocks support migrations\n const supportsStateMigrations = newConfig.modelAPIVersion === BLOCK_STORAGE_FACADE_VERSION;\n\n if (supportsStateMigrations) {\n const currentStorageJson = info.blockStorageJson;\n\n // Attempt migration BEFORE persisting block pack — on failure,\n // block stays on old version (no inconsistent new-code/old-storage state)\n const migrationResult = this.projectHelper.migrateStorageInVM(\n newConfig,\n currentStorageJson,\n );\n\n if (migrationResult.error !== undefined) {\n throw new Error(\n `[migrateBlockPack] Block ${blockId} migration failed: ${migrationResult.error}`,\n );\n }\n\n this.projectHelper.logger.info(\n `[migrateBlockPack] Block ${blockId}: ${migrationResult.info}`,\n );\n applyStorageAndDeriveArgs(migrationResult.newStorageJson);\n } else {\n // Legacy blocks (modelAPIVersion 1): persist block pack, set prerunArgs = currentArgs\n persistBlockPack();\n if (info.fields.currentArgs !== undefined) {\n this.setBlockFieldObj(blockId, \"currentPrerunArgs\", info.fields.currentArgs);\n }\n }\n\n this.blocksWithChangedInputs.add(blockId);\n\n // resetting staging outputs for all downstream blocks\n this.getStagingGraph().traverse(\"downstream\", [blockId], ({ id }) => this.resetStaging(id));\n }\n\n // also reset or limbo all downstream productions\n if (info.productionRendered)\n this.getActualProductionGraph().traverse(\"downstream\", [blockId], ({ id }) =>\n this.resetOrLimboProduction(id),\n );\n\n this.updateLastModified();\n }\n\n //\n // Render\n //\n\n public renderProduction(blockIds: string[], addUpstreams: boolean = false): Set<string> {\n const blockIdsSet = new Set(blockIds);\n\n const prodGraph = this.getPendingProductionGraph();\n if (addUpstreams)\n // adding all upstreams automatically\n prodGraph.traverse(\"upstream\", blockIds, (node) => {\n blockIdsSet.add(node.id);\n });\n else\n // checking that targets contain all upstreams\n for (const blockId of blockIdsSet) {\n const node = prodGraph.nodes.get(blockId);\n if (node === undefined) throw new Error(`Can't find block with id: ${blockId}`);\n for (const upstream of node.upstream)\n if (!blockIdsSet.has(upstream))\n throw new Error(\"Can't render blocks not including all upstreams.\");\n }\n\n // traversing in topological order and rendering target blocks\n const rendered = new Set<string>();\n for (const block of allBlocks(this.structure)) {\n if (!blockIdsSet.has(block.id)) continue;\n\n let render =\n this.getBlockInfo(block.id).requireProductionRendering || this.blocksInLimbo.has(block.id);\n\n if (!render)\n for (const upstream of prodGraph.nodes.get(block.id)!.upstream)\n if (rendered.has(upstream)) {\n render = true;\n break;\n }\n\n if (render) {\n this.renderProductionFor(block.id);\n rendered.add(block.id);\n }\n }\n\n const renderedArray = [...rendered];\n\n // sending to limbo all downstream blocks\n prodGraph.traverse(\"downstream\", renderedArray, (node) => {\n if (rendered.has(node.id))\n // don't send to limbo blocks that were just rendered\n return;\n this.resetOrLimboProduction(node.id);\n });\n\n // resetting staging outputs for all downstream blocks\n this.getStagingGraph().traverse(\"downstream\", renderedArray, ({ id }) => {\n // don't reset staging of the first rendered block\n if (renderedArray[0] !== id) this.resetStaging(id);\n });\n\n if (rendered.size > 0) this.updateLastModified();\n\n return rendered;\n }\n\n /** Stops running blocks from the list and modify states of other blocks\n * accordingly */\n public stopProduction(...blockIds: string[]) {\n const activeProdGraph = this.getActualProductionGraph();\n\n // we will stop all blocks listed in request and all their downstreams\n const queue = new Denque(blockIds);\n const queued = new Set(blockIds);\n const stopped: string[] = [];\n\n while (!queue.isEmpty()) {\n const blockId = queue.shift()!;\n const fields = this.getBlockInfo(blockId).fields;\n\n if (fields.prodOutput?.status === \"Ready\" && fields.prodCtx?.status === \"Ready\")\n // skipping finished blocks\n continue;\n\n if (this.deleteBlockFields(blockId, \"prodOutput\", \"prodCtx\", \"prodUiCtx\", \"prodArgs\")) {\n // was actually stopped\n stopped.push(blockId);\n\n // will try to stop all its downstreams\n for (const downstream of activeProdGraph.traverseIdsExcludingRoots(\"downstream\", blockId)) {\n if (queued.has(downstream)) continue;\n queue.push(downstream);\n queued.add(downstream);\n }\n }\n }\n\n // blocks under stopped blocks, but having finished production results, goes to limbo\n for (const blockId of activeProdGraph.traverseIdsExcludingRoots(\"downstream\", ...stopped))\n this.resetOrLimboProduction(blockId);\n\n // reset staging outputs for all downstream blocks\n this.getStagingGraph().traverse(\"downstream\", stopped, ({ id }) => this.resetStaging(id));\n }\n\n private traverseWithStagingLag(cb: (blockId: string, lag: number) => void) {\n const lags = new Map<string, number>();\n const stagingGraph = this.getStagingGraph();\n stagingGraph.nodes.forEach((node) => {\n const info = this.getBlockInfo(node.id);\n // Use requireStagingRendering to check both: staging exists AND prerunArgs hasn't changed\n const requiresRendering = info.requireStagingRendering;\n let lag = requiresRendering ? 1 : 0;\n node.upstream.forEach((upstream) => {\n const upstreamLag = lags.get(upstream)!;\n if (upstreamLag === 0) return;\n lag = Math.max(upstreamLag + 1, lag);\n });\n if (!requiresRendering && info.stagingRendered) {\n // console.log(`[traverseWithStagingLag] SKIP staging for ${node.id} - prerunArgs unchanged`);\n }\n cb(node.id, lag);\n lags.set(node.id, lag);\n });\n }\n\n /** @param stagingRenderingRate rate in blocks per second */\n private refreshStagings(stagingRenderingRate?: number) {\n const elapsed = Date.now() - this.renderingState.stagingRefreshTimestamp;\n const lagThreshold =\n stagingRenderingRate === undefined\n ? undefined\n : 1 + Math.max(0, (elapsed * stagingRenderingRate) / 1000);\n let rendered = 0;\n this.traverseWithStagingLag((blockId, lag) => {\n if (lag === 0)\n // meaning staging already rendered\n return;\n if (lagThreshold === undefined || lag <= lagThreshold) {\n try {\n this.renderStagingFor(blockId);\n rendered++;\n } catch (e) {\n this.projectHelper.logger.error(\n new Error(`[refreshStagings] renderStagingFor failed for ${blockId}`, { cause: e }),\n );\n }\n }\n });\n if (rendered > 0) this.resetStagingRefreshTimestamp();\n }\n\n //\n // Meta\n //\n\n /** Updates project metadata */\n public setMeta(meta: ProjectMeta): void {\n this.meta = meta;\n this.metaChanged = true;\n this.updateLastModified();\n }\n\n //\n // Maintenance\n //\n\n /** @param stagingRenderingRate rate in blocks per second */\n public doRefresh(stagingRenderingRate?: number) {\n this.refreshStagings(stagingRenderingRate);\n this.blockInfos.forEach((blockInfo) => {\n if (\n blockInfo.fields.prodCtx?.status === \"Ready\" &&\n blockInfo.fields.prodOutput?.status === \"Ready\"\n )\n this.deleteBlockFields(\n blockInfo.id,\n \"prodOutputPrevious\",\n \"prodCtxPrevious\",\n \"prodUiCtxPrevious\",\n );\n if (\n blockInfo.fields.stagingCtx?.status === \"Ready\" &&\n blockInfo.fields.stagingOutput?.status === \"Ready\"\n )\n this.deleteBlockFields(\n blockInfo.id,\n \"stagingOutputPrevious\",\n \"stagingCtxPrevious\",\n \"stagingUiCtxPrevious\",\n );\n });\n }\n\n private assignAuthorMarkers() {\n const markerStr = this.author ? JSON.stringify(this.author) : undefined;\n\n for (const blockId of this.blocksWithChangedInputs)\n if (markerStr === undefined) this.tx.deleteKValue(this.rid, blockArgsAuthorKey(blockId));\n else this.tx.setKValue(this.rid, blockArgsAuthorKey(blockId), markerStr);\n\n if (this.metaChanged || this.structureChanged) {\n if (markerStr === undefined) this.tx.deleteKValue(this.rid, ProjectStructureAuthorKey);\n else this.tx.setKValue(this.rid, ProjectStructureAuthorKey, markerStr);\n }\n }\n\n public save() {\n if (!this.wasModified) return;\n\n if (this.lastModifiedChanged)\n this.tx.setKValue(this.rid, ProjectLastModifiedTimestamp, JSON.stringify(this.lastModified));\n\n if (this.structureChanged)\n this.tx.setKValue(this.rid, ProjectStructureKey, JSON.stringify(this.struct));\n\n if (this.renderingStateChanged)\n this.tx.setKValue(\n this.rid,\n BlockRenderingStateKey,\n JSON.stringify({\n ...this.renderingState,\n blocksInLimbo: [...this.blocksInLimbo],\n } as ProjectRenderingState),\n );\n\n if (this.metaChanged) this.tx.setKValue(this.rid, ProjectMetaKey, JSON.stringify(this.meta));\n\n this.assignAuthorMarkers();\n }\n\n public static async load(\n projectHelper: ProjectHelper,\n tx: PlTransaction,\n rid: ResourceId,\n author?: AuthorMarker,\n ): Promise<ProjectMutator> {\n //\n // Sending initial requests to read project state (start of round-trip #1)\n //\n\n const fullResourceStateP = tx.getResourceData(rid, true);\n const schemaP = tx.getKValueJson<string>(rid, SchemaVersionKey);\n const lastModifiedP = tx.getKValueJson<number>(rid, ProjectLastModifiedTimestamp);\n const metaP = tx.getKValueJson<ProjectMeta>(rid, ProjectMetaKey);\n const structureP = tx.getKValueJson<ProjectStructure>(rid, ProjectStructureKey);\n const renderingStateP = tx.getKValueJson<ProjectRenderingState>(rid, BlockRenderingStateKey);\n\n const fullResourceState = await fullResourceStateP;\n\n // loading field information\n const blockInfoStates = new Map<string, BlockInfoState>();\n for (const f of fullResourceState.fields) {\n const projectField = parseProjectField(f.name);\n\n // processing only fields with known structure\n if (projectField === undefined) continue;\n\n let info = blockInfoStates.get(projectField.blockId);\n if (info === undefined) {\n info = {\n id: projectField.blockId,\n fields: {},\n };\n blockInfoStates.set(projectField.blockId, info);\n }\n\n info.fields[projectField.fieldName] = isNullResourceId(f.value)\n ? { modCount: 0 }\n : { modCount: 0, ref: f.value };\n }\n\n //\n // Roundtrip #1 not yet finished, but as soon as field list is received,\n // we can start sending requests to read states of referenced resources\n // (start of round-trip #2)\n //\n\n const blockFieldRequests: [\n BlockInfoState,\n ProjectField[\"fieldName\"],\n BlockFieldState,\n Promise<BasicResourceData | ResourceData>,\n ][] = [];\n blockInfoStates.forEach((info) => {\n const fields = info.fields;\n for (const [fName, state] of Object.entries(fields)) {\n if (state.ref === undefined) continue;\n if (!isResource(state.ref) || isResourceRef(state.ref))\n throw new Error(\"unexpected behaviour\");\n const fieldName = fName as ProjectField[\"fieldName\"];\n blockFieldRequests.push([\n info,\n fieldName,\n state,\n tx.getResourceData(state.ref, fieldName == \"blockPack\"),\n ]);\n }\n });\n\n // loading jsons\n const [schema, lastModified, meta, structure, { stagingRefreshTimestamp, blocksInLimbo }] =\n await Promise.all([schemaP, lastModifiedP, metaP, structureP, renderingStateP]);\n\n // Checking schema version of the project\n if (schema !== SchemaVersionCurrent) {\n if (Number(schema) < Number(SchemaVersionCurrent))\n throw new UiError(\n `Can't perform this action on this project because it has older schema. Try (re)loading the project to update it.`,\n );\n else\n throw new UiError(\n `Can't perform this action on this project because it has newer schema. Upgrade your desktop app to the latest version.`,\n );\n }\n\n //\n // <- at this point we have all the responses from round-trip #1\n //\n\n //\n // Receiving responses from round-trip #2 and sending requests to read block pack descriptions\n // (start of round-trip #3)\n //\n\n const blockPackRequests: [BlockInfoState, Promise<BasicResourceData>][] = [];\n for (const [info, fieldName, state, response] of blockFieldRequests) {\n const result = await response;\n state.value = result.data;\n if (isNotNullResourceId(result.error)) state.status = \"Error\";\n else if (result.resourceReady || isNotNullResourceId(result.originalResourceId))\n state.status = \"Ready\";\n else state.status = \"NotReady\";\n\n // For block pack we need to traverse the ref field from the resource data\n if (fieldName === \"blockPack\") {\n const refField = (result as ResourceData).fields.find((f) => f.name === Pl.HolderRefField);\n if (refField === undefined) throw new Error(\"Block pack ref field is missing\");\n blockPackRequests.push([\n info,\n tx.getResourceData(ensureResourceIdNotNull(refField.value), false),\n ]);\n }\n }\n\n //\n // <- at this point we have all the responses from round-trip #2\n //\n\n for (const [info, response] of blockPackRequests) {\n const result = await response;\n const bpInfo = cachedDeserialize<BlockPackInfo>(notEmpty(result.data));\n info.blockConfig = extractConfig(bpInfo.config);\n info.blockPack = bpInfo.source;\n }\n\n //\n // <- at this point we have all the responses from round-trip #3\n //\n\n // loading ctx export template to check if we already have cached materialized template in our project\n const ctxExportTplEnvelope = await getPreparedExportTemplateEnvelope();\n\n // expected field name\n const ctxExportTplCacheFieldName = getServiceTemplateField(ctxExportTplEnvelope.hash);\n const ctxExportTplField = fullResourceState.fields.find(\n (f) => f.name === ctxExportTplCacheFieldName,\n );\n let ctxExportTplHolder: AnyResourceRef;\n if (ctxExportTplField !== undefined)\n ctxExportTplHolder = ensureResourceIdNotNull(ctxExportTplField.value);\n else {\n ctxExportTplHolder = Pl.wrapInHolder(tx, loadTemplate(tx, ctxExportTplEnvelope.spec));\n tx.createField(\n field(rid, getServiceTemplateField(ctxExportTplEnvelope.hash)),\n \"Dynamic\",\n ctxExportTplHolder,\n );\n }\n\n const renderingState = { stagingRefreshTimestamp };\n const blocksInLimboSet = new Set(blocksInLimbo);\n\n const blockInfos = new Map<string, BlockInfo>();\n blockInfoStates.forEach(({ id, fields, blockConfig, blockPack }) =>\n blockInfos.set(\n id,\n new BlockInfo(id, fields, notEmpty(blockConfig), notEmpty(blockPack), projectHelper.logger),\n ),\n );\n\n // check consistency of project state\n const blockInStruct = new Set<string>();\n for (const b of allBlocks(structure)) {\n if (!blockInfos.has(b.id))\n throw new Error(`Inconsistent project structure: no inputs for ${b.id}`);\n blockInStruct.add(b.id);\n }\n blockInfos.forEach((info) => {\n if (!blockInStruct.has(info.id))\n throw new Error(`Inconsistent project structure: no structure entry for ${info.id}`);\n // checking structure\n info.check();\n });\n\n const prj = new ProjectMutator(\n rid,\n tx,\n author,\n schema,\n lastModified,\n meta,\n structure,\n renderingState,\n blocksInLimboSet,\n blockInfos,\n ctxExportTplHolder,\n projectHelper,\n );\n\n prj.fixProblemsAndMigrate();\n\n return prj;\n }\n}\n\nexport interface ProjectState {\n schema: string;\n structure: ProjectStructure;\n renderingState: Omit<ProjectRenderingState, \"blocksInLimbo\">;\n blocksInLimbo: Set<string>;\n blockInfos: Map<string, BlockInfo>;\n}\n\nexport async function createProject(\n tx: PlTransaction,\n meta: ProjectMeta = InitialBlockMeta,\n): Promise<AnyResourceRef> {\n const prj = tx.createEphemeral(ProjectResourceType);\n tx.lock(prj);\n const ts = String(Date.now());\n tx.setKValue(prj, SchemaVersionKey, JSON.stringify(SchemaVersionCurrent));\n tx.setKValue(prj, ProjectCreatedTimestamp, ts);\n tx.setKValue(prj, ProjectLastModifiedTimestamp, ts);\n tx.setKValue(prj, ProjectMetaKey, JSON.stringify(meta));\n tx.setKValue(prj, ProjectStructureKey, JSON.stringify(InitialBlockStructure));\n tx.setKValue(prj, BlockRenderingStateKey, JSON.stringify(InitialProjectRenderingState));\n const ctxExportTplEnvelope = await getPreparedExportTemplateEnvelope();\n tx.createField(\n field(prj, getServiceTemplateField(ctxExportTplEnvelope.hash)),\n \"Dynamic\",\n Pl.wrapInHolder(tx, loadTemplate(tx, ctxExportTplEnvelope.spec)),\n );\n return prj;\n}\n\nexport async function withProject<T>(\n projectHelper: ProjectHelper,\n txOrPl: PlTransaction | PlClient,\n rid: ResourceId,\n cb: (p: ProjectMutator) => T | Promise<T>,\n ops?: Partial<TxOps>,\n): Promise<T> {\n return withProjectAuthored(projectHelper, txOrPl, rid, undefined, cb, ops);\n}\n\nexport async function withProjectAuthored<T>(\n projectHelper: ProjectHelper,\n txOrPl: PlTransaction | PlClient,\n rid: ResourceId,\n author: AuthorMarker | undefined,\n cb: (p: ProjectMutator) => T | Promise<T>,\n ops: Partial<TxOps> = {},\n): Promise<T> {\n if (txOrPl instanceof PlClient) {\n return await txOrPl.withWriteTx(\n \"ProjectAction\" + (ops.name ? `: ${ops.name}` : \"\"),\n async (tx) => {\n const mut = await ProjectMutator.load(projectHelper, tx, rid, author);\n const result = await cb(mut);\n if (!mut.wasModified)\n // skipping save and commit altogether if no modifications were actually made\n return result;\n mut.save();\n await tx.commit();\n if (getDebugFlags().logProjectMutationStat) console.log(JSON.stringify(tx.stat));\n return result;\n },\n ops,\n );\n } else {\n const mut = await ProjectMutator.load(projectHelper, txOrPl, rid, author);\n const result = await cb(mut);\n mut.save();\n return result;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAiGA,SAAS,OAAiB,SAAsB,SAA2B;CACzE,IAAI,cAAc;CAClB,IAAI,YAA+B;CACnC,IAAI,QAAuB;AAC3B,cAAa;AACX,MAAI,CAAC,aAAa;AAChB,iBAAc;AACd,eAAY,SAAS;AACrB,WAAQ,SAAS;AACjB,UAAO;;EAET,MAAM,eAAe,SAAS;AAC9B,MAAI,cAAc,cAAc;AAC9B,eAAY;AACZ,WAAQ,SAAS;;AAEnB,SAAO,SAAS;;;AAIpB,IAAM,YAAN,MAAgB;CACd,YACE,AAAgB,IAChB,AAAgB,QAChB,AAAgB,QAChB,AAAgB,QAChB,AAAiB,SAAmB,IAAIA,iDAAsB,EAC9D;EALgB;EACA;EACA;EACA;EACC;;CAGnB,AAAO,QAAQ;AAGb,MAAK,KAAK,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AACV,QAAM,GAAG,QAAQ;AACjB,MAAIC,6BAAe,CAAC,uBAAwB,SAAQ,IAAI,KAAK,UAAU,GAAG,KAAK,CAAC;AAChF,SAAO;IAET,IACD;MACI;EACL,MAAM,MAAM,MAAM,eAAe,KAAK,eAAe,QAAQ,KAAK,OAAO;EACzE,MAAM,SAAS,MAAM,GAAG,IAAI;AAC5B,MAAI,MAAM;AACV,SAAO"}
|
|
1
|
+
{"version":3,"file":"project.cjs","names":["ConsoleLoggerAdapter","Pl","BlockPackTemplateField","InitialBlockSettings","stagingGraph","productionGraph","allBlocks","projectFieldName","BLOCK_STORAGE_FACADE_VERSION","createBContextFromUpstreams","exportContext","createRenderHeavyBlock","createBlockPack","FieldsToDuplicate","graphDiff","Denque","blockArgsAuthorKey","ProjectStructureAuthorKey","ProjectLastModifiedTimestamp","ProjectStructureKey","BlockRenderingStateKey","ProjectMetaKey","SchemaVersionKey","parseProjectField","SchemaVersionCurrent","UiError","getPreparedExportTemplateEnvelope","getServiceTemplateField","loadTemplate","InitialBlockMeta","ProjectResourceType","ProjectCreatedTimestamp","InitialBlockStructure","InitialProjectRenderingState","PlClient","getDebugFlags"],"sources":["../../src/mutator/project.ts"],"sourcesContent":["import type {\n AnyRef,\n AnyResourceRef,\n BasicResourceData,\n PlTransaction,\n ResourceData,\n ResourceId,\n TxOps,\n} from \"@milaboratories/pl-client\";\nimport {\n ensureResourceIdNotNull,\n field,\n isNotNullResourceId,\n isNullResourceId,\n isResource,\n isResourceId,\n isResourceRef,\n Pl,\n PlClient,\n} from \"@milaboratories/pl-client\";\nimport { createRenderHeavyBlock, createBContextFromUpstreams } from \"./template/render_block\";\nimport type {\n Block,\n ProjectStructure,\n ProjectField,\n ProjectRenderingState,\n} from \"../model/project_model\";\nimport {\n BlockRenderingStateKey,\n ProjectStructureKey,\n parseProjectField,\n projectFieldName,\n SchemaVersionCurrent,\n SchemaVersionKey,\n ProjectResourceType,\n InitialBlockStructure,\n InitialProjectRenderingState,\n ProjectMetaKey,\n InitialBlockMeta,\n blockArgsAuthorKey,\n ProjectLastModifiedTimestamp,\n ProjectCreatedTimestamp,\n ProjectStructureAuthorKey,\n getServiceTemplateField,\n FieldsToDuplicate,\n} from \"../model/project_model\";\nimport { BlockPackTemplateField, createBlockPack } from \"./block-pack/block_pack\";\nimport type { BlockGraph, ProductionGraphBlockInfo } from \"../model/project_model_util\";\nimport { allBlocks, graphDiff, productionGraph, stagingGraph } from \"../model/project_model_util\";\nimport type { BlockPackSpecPrepared } from \"../model\";\nimport type {\n AuthorMarker,\n BlockPackSpec,\n BlockSettings,\n ProjectMeta,\n} from \"@milaboratories/pl-model-middle-layer\";\nimport { InitialBlockSettings } from \"@milaboratories/pl-model-middle-layer\";\nimport Denque from \"denque\";\nimport { exportContext, getPreparedExportTemplateEnvelope } from \"./context_export\";\nimport { loadTemplate } from \"./template/template_loading\";\nimport {\n cachedDeserialize,\n notEmpty,\n canonicalJsonBytes,\n cachedDecode,\n type MiLogger,\n ConsoleLoggerAdapter,\n} from \"@milaboratories/ts-helpers\";\nimport type { ProjectHelper } from \"../model/project_helper\";\nimport {\n extractConfig,\n UiError,\n BLOCK_STORAGE_FACADE_VERSION,\n type BlockConfig,\n} from \"@platforma-sdk/model\";\nimport { getDebugFlags } from \"../debug\";\nimport type { BlockPackInfo } from \"../model/block_pack\";\n\ntype FieldStatus = \"NotReady\" | \"Ready\" | \"Error\";\n\ninterface BlockFieldState {\n modCount: number;\n ref?: AnyRef;\n status?: FieldStatus;\n value?: Uint8Array;\n}\n\ntype BlockFieldStates = Partial<Record<ProjectField[\"fieldName\"], BlockFieldState>>;\ntype BlockFieldStateValue = Omit<BlockFieldState, \"modCount\">;\n\ninterface BlockInfoState {\n readonly id: string;\n readonly fields: BlockFieldStates;\n blockConfig?: BlockConfig;\n blockPack?: BlockPackSpec;\n}\n\nfunction cached<ModId, T>(modIdCb: () => ModId, valueCb: () => T): () => T {\n let initialized = false;\n let lastModId: ModId | undefined = undefined;\n let value: T | undefined = undefined;\n return () => {\n if (!initialized) {\n initialized = true;\n lastModId = modIdCb();\n value = valueCb();\n return value;\n }\n const currentModId = modIdCb();\n if (lastModId !== currentModId) {\n lastModId = currentModId;\n value = valueCb();\n }\n return valueCb();\n };\n}\n\nclass BlockInfo {\n constructor(\n public readonly id: string,\n public readonly fields: BlockFieldStates,\n public readonly config: BlockConfig,\n public readonly source: BlockPackSpec,\n private readonly logger: MiLogger = new ConsoleLoggerAdapter(),\n ) {}\n\n public check() {\n // state assertions\n\n if ((this.fields.prodOutput === undefined) !== (this.fields.prodCtx === undefined))\n throw new Error(\"inconsistent prod fields\");\n\n if ((this.fields.stagingOutput === undefined) !== (this.fields.stagingCtx === undefined))\n throw new Error(\"inconsistent stage fields\");\n\n if (\n (this.fields.prodOutputPrevious === undefined) !==\n (this.fields.prodCtxPrevious === undefined)\n )\n throw new Error(\"inconsistent prod cache fields\");\n\n if (\n (this.fields.stagingOutputPrevious === undefined) !==\n (this.fields.stagingCtxPrevious === undefined)\n )\n throw new Error(\"inconsistent stage cache fields\");\n\n if (this.fields.blockPack === undefined) throw new Error(\"no block pack field\");\n\n if (this.fields.blockStorage === undefined) throw new Error(\"no block storage field\");\n }\n\n private readonly currentArgsC = cached(\n () => this.fields.currentArgs?.modCount,\n () => {\n const bin = this.fields.currentArgs?.value;\n if (bin === undefined) return undefined;\n return cachedDeserialize(bin);\n },\n );\n\n private readonly blockStorageC = cached(\n () => this.fields.blockStorage!.modCount,\n () => {\n const bin = this.fields.blockStorage?.value;\n if (bin === undefined) return undefined;\n return cachedDeserialize<Record<string, unknown>>(bin);\n },\n );\n\n private readonly blockStorageJ = cached(\n () => this.fields.blockStorage!.modCount,\n () => {\n const bin = this.fields.blockStorage?.value;\n if (bin === undefined) return undefined;\n return cachedDecode(bin);\n },\n );\n\n private readonly prodArgsC = cached(\n () => this.fields.prodArgs?.modCount,\n () => {\n const bin = this.fields.prodArgs?.value;\n if (bin === undefined) return undefined;\n return cachedDeserialize(bin);\n },\n );\n\n private readonly currentPrerunArgsC = cached(\n () => this.fields.currentPrerunArgs?.modCount,\n () => {\n const bin = this.fields.currentPrerunArgs?.value;\n if (bin === undefined) return undefined;\n return cachedDeserialize(bin);\n },\n );\n\n get currentArgs(): unknown {\n return this.currentArgsC();\n }\n\n get blockStorage(): unknown {\n try {\n return this.blockStorageC();\n } catch (e) {\n this.logger.error(new Error(`Error getting blockStorage for ${this.id}`, { cause: e }));\n return undefined;\n }\n }\n\n get blockStorageJson() {\n return this.blockStorageJ();\n }\n\n get currentPrerunArgs(): unknown {\n return this.currentPrerunArgsC();\n }\n\n get stagingRendered(): boolean {\n return this.fields.stagingCtx !== undefined;\n }\n\n get productionRendered(): boolean {\n return this.fields.prodCtx !== undefined;\n }\n\n get productionHasErrors(): boolean {\n return this.fields.prodUiCtx?.status === \"Error\";\n }\n\n private readonly productionStaleC: () => boolean = cached(\n () => `${this.fields.currentArgs!.modCount}_${this.fields.prodArgs?.modCount}`,\n () =>\n this.fields.prodArgs === undefined ||\n Buffer.compare(this.fields.currentArgs!.value!, this.fields.prodArgs.value!) !== 0,\n );\n\n get requireProductionRendering(): boolean {\n return !this.productionRendered || this.productionStaleC() || this.productionHasErrors;\n }\n\n /** Returns true if staging should be re-rendered (stagingCtx is not set) */\n get requireStagingRendering(): boolean {\n // No staging needed if currentPrerunArgs is undefined (args derivation failed)\n if (this.fields.currentPrerunArgs === undefined) return false;\n return !this.stagingRendered;\n }\n\n get prodArgs(): unknown {\n return this.prodArgsC();\n }\n\n public getTemplate(tx: PlTransaction): AnyRef {\n return tx.getFutureFieldValue(\n Pl.unwrapHolder(tx, this.fields.blockPack!.ref!),\n BlockPackTemplateField,\n \"Input\",\n );\n }\n}\n\n/**\n * Specification for creating a new block.\n * Discriminated union based on `storageMode`.\n */\n/** Specification for creating a new block. Discriminated union based on `storageMode`. */\nexport type NewBlockSpec =\n | { storageMode: \"fromModel\"; blockPack: BlockPackSpecPrepared }\n | { storageMode: \"legacy\"; blockPack: BlockPackSpecPrepared; legacyState: string };\n\nconst NoNewBlocks = (blockId: string) => {\n throw new Error(`No new block info for ${blockId}`);\n};\n\n/**\n * Request to set block state using unified state format.\n * For v3 blocks: state is the block's state\n * For v1/v2 blocks: state should be { args, uiState } format\n */\nexport type SetStatesRequest =\n | {\n blockId: string;\n /** The unified state to set */\n state: unknown;\n modelAPIVersion: 1;\n }\n | {\n blockId: string;\n /** Storage operation payload - middle layer is agnostic to specific operations */\n payload: { operation: string; value: unknown };\n modelAPIVersion: 2;\n };\n\nexport type ClearState = {\n state: unknown;\n};\n\nexport class ProjectMutator {\n private globalModCount = 0;\n private fieldsChanged: boolean = false;\n\n //\n // Change trackers\n //\n\n private lastModifiedChanged = false;\n private structureChanged = false;\n private metaChanged = false;\n private renderingStateChanged = false;\n\n /** Set blocks will be assigned current mutator author marker on save */\n private readonly blocksWithChangedInputs = new Set<string>();\n\n constructor(\n public readonly rid: ResourceId,\n private readonly tx: PlTransaction,\n private readonly author: AuthorMarker | undefined,\n private readonly schema: string,\n private lastModified: number,\n private meta: ProjectMeta,\n private struct: ProjectStructure,\n private readonly renderingState: Omit<ProjectRenderingState, \"blocksInLimbo\">,\n private readonly blocksInLimbo: Set<string>,\n private readonly blockInfos: Map<string, BlockInfo>,\n private readonly ctxExportTplHolder: AnyResourceRef,\n private readonly projectHelper: ProjectHelper,\n ) {}\n\n private fixProblemsAndMigrate() {\n // Fix inconsistent production fields\n this.blockInfos.forEach((blockInfo) => {\n if (\n blockInfo.fields.prodArgs === undefined ||\n blockInfo.fields.prodOutput === undefined ||\n blockInfo.fields.prodCtx === undefined\n )\n this.deleteBlockFields(blockInfo.id, \"prodArgs\", \"prodOutput\", \"prodCtx\", \"prodUiCtx\");\n });\n\n // Fix inconsistent staging fields\n this.blockInfos.forEach((blockInfo) => {\n if (blockInfo.fields.stagingOutput === undefined || blockInfo.fields.stagingCtx === undefined)\n this.deleteBlockFields(blockInfo.id, \"stagingOutput\", \"stagingCtx\", \"stagingUiCtx\");\n });\n\n // Fix inconsistent cache fields\n this.blockInfos.forEach((blockInfo) => {\n if (\n blockInfo.fields.prodOutputPrevious === undefined ||\n blockInfo.fields.prodCtxPrevious === undefined\n )\n this.deleteBlockFields(\n blockInfo.id,\n \"prodOutputPrevious\",\n \"prodCtxPrevious\",\n \"prodUiCtxPrevious\",\n );\n if (\n blockInfo.fields.stagingOutputPrevious === undefined ||\n blockInfo.fields.stagingCtxPrevious === undefined\n )\n this.deleteBlockFields(\n blockInfo.id,\n \"stagingOutputPrevious\",\n \"stagingCtxPrevious\",\n \"stagingUiCtxPrevious\",\n );\n });\n\n // Migration for addition of block settings field\n let initialBlockSettings: Omit<BlockFieldState, \"modCount\"> | undefined;\n this.blockInfos.forEach((blockInfo) => {\n if (blockInfo.fields.blockSettings === undefined) {\n if (initialBlockSettings === undefined)\n initialBlockSettings = this.createJsonFieldValue(InitialBlockSettings);\n this.setBlockFieldObj(blockInfo.id, \"blockSettings\", initialBlockSettings);\n }\n });\n\n // Validate after fixes\n this.blockInfos.forEach((info) => info.check());\n }\n\n get wasModified(): boolean {\n return (\n this.lastModifiedChanged ||\n this.structureChanged ||\n this.fieldsChanged ||\n this.metaChanged ||\n this.renderingStateChanged\n );\n }\n\n get structure(): ProjectStructure {\n return JSON.parse(JSON.stringify(this.struct)) as ProjectStructure;\n }\n\n //\n // Graph calculation\n //\n\n private stagingGraph: BlockGraph | undefined = undefined;\n private pendingProductionGraph: BlockGraph | undefined = undefined;\n private actualProductionGraph: BlockGraph | undefined = undefined;\n\n private getStagingGraph(): BlockGraph {\n if (this.stagingGraph === undefined) this.stagingGraph = stagingGraph(this.struct);\n return this.stagingGraph;\n }\n\n private getProductionGraphBlockInfo(\n blockId: string,\n prod: boolean,\n ): ProductionGraphBlockInfo | undefined {\n const bInfo = this.getBlockInfo(blockId);\n\n let argsField: BlockFieldState | undefined;\n let args: unknown;\n\n if (prod) {\n if (bInfo.fields.prodArgs === undefined) return undefined;\n argsField = bInfo.fields.prodArgs;\n args = bInfo.prodArgs;\n } else {\n argsField = bInfo.fields.currentArgs;\n args = bInfo.currentArgs;\n }\n\n // Can't compute enrichment targets without args\n if (argsField === undefined) {\n return { args, enrichmentTargets: undefined };\n }\n\n const blockPackField = notEmpty(bInfo.fields.blockPack);\n\n if (isResourceId(argsField.ref!) && isResourceId(blockPackField.ref!))\n return {\n args,\n enrichmentTargets: this.projectHelper.getEnrichmentTargets(\n () => bInfo.config,\n () => args,\n { argsRid: argsField.ref, blockPackRid: blockPackField.ref },\n ),\n };\n else\n return {\n args,\n enrichmentTargets: this.projectHelper.getEnrichmentTargets(\n () => bInfo.config,\n () => args,\n ),\n };\n }\n\n private getPendingProductionGraph(): BlockGraph {\n if (this.pendingProductionGraph === undefined)\n this.pendingProductionGraph = productionGraph(this.struct, (blockId) =>\n this.getProductionGraphBlockInfo(blockId, false),\n );\n return this.pendingProductionGraph;\n }\n\n private getActualProductionGraph(): BlockGraph {\n if (this.actualProductionGraph === undefined)\n this.actualProductionGraph = productionGraph(this.struct, (blockId) =>\n this.getProductionGraphBlockInfo(blockId, true),\n );\n return this.actualProductionGraph;\n }\n\n //\n // Generic helpers to interact with project state\n //\n\n private getBlockInfo(blockId: string): BlockInfo {\n const info = this.blockInfos.get(blockId);\n if (info === undefined) throw new Error(`No such block: ${blockId}`);\n return info;\n }\n\n private createJsonFieldValueByContent(content: string): BlockFieldStateValue {\n if (content === undefined) throw new Error(\"content is undefined\");\n const value = Buffer.from(content);\n const ref = this.tx.createValue(Pl.JsonObject, value);\n return { ref, value, status: \"Ready\" };\n }\n\n private createJsonFieldValue(obj: unknown): BlockFieldStateValue {\n return this.createJsonFieldValueByContent(JSON.stringify(obj));\n }\n\n private getBlock(blockId: string): Block {\n for (const block of allBlocks(this.struct)) if (block.id === blockId) return block;\n throw new Error(\"block not found\");\n }\n\n private setBlockFieldObj(\n blockId: string,\n fieldName: keyof BlockFieldStates,\n state: BlockFieldStateValue,\n ) {\n const fid = field(this.rid, projectFieldName(blockId, fieldName));\n\n if (state.ref === undefined) throw new Error(\"Can't set value with empty ref\");\n\n if (this.getBlockInfo(blockId).fields[fieldName] === undefined)\n this.tx.createField(fid, \"Dynamic\", state.ref);\n else this.tx.setField(fid, state.ref);\n\n this.getBlockInfo(blockId).fields[fieldName] = {\n modCount: this.globalModCount++,\n ...state,\n };\n\n this.fieldsChanged = true;\n }\n\n private setBlockField(\n blockId: string,\n fieldName: keyof BlockFieldStates,\n ref: AnyRef,\n status: FieldStatus,\n value?: Uint8Array,\n ) {\n this.setBlockFieldObj(blockId, fieldName, { ref, status, value });\n }\n\n private deleteBlockFields(blockId: string, ...fieldNames: (keyof BlockFieldStates)[]): boolean {\n let deleted = false;\n const info = this.getBlockInfo(blockId);\n for (const fieldName of fieldNames) {\n const fields = info.fields;\n if (!(fieldName in fields)) continue;\n this.tx.removeField(field(this.rid, projectFieldName(blockId, fieldName)));\n delete fields[fieldName];\n this.fieldsChanged = true;\n deleted = true;\n }\n return deleted;\n }\n\n private updateLastModified() {\n this.lastModified = Date.now();\n this.lastModifiedChanged = true;\n }\n\n //\n // Main project actions\n //\n\n private resetStagingRefreshTimestamp() {\n this.renderingState.stagingRefreshTimestamp = Date.now();\n this.renderingStateChanged = true;\n }\n\n private resetStaging(blockId: string): void {\n const fields = this.getBlockInfo(blockId).fields;\n if (\n fields.stagingOutput?.status === \"Ready\" &&\n fields.stagingCtx?.status === \"Ready\" &&\n fields.stagingUiCtx?.status === \"Ready\"\n ) {\n this.setBlockFieldObj(blockId, \"stagingOutputPrevious\", fields.stagingOutput);\n this.setBlockFieldObj(blockId, \"stagingCtxPrevious\", fields.stagingCtx);\n this.setBlockFieldObj(blockId, \"stagingUiCtxPrevious\", fields.stagingUiCtx);\n }\n if (this.deleteBlockFields(blockId, \"stagingOutput\", \"stagingCtx\", \"stagingUiCtx\"))\n this.resetStagingRefreshTimestamp();\n }\n\n private resetProduction(blockId: string): void {\n const fields = this.getBlockInfo(blockId).fields;\n if (\n fields.prodOutput?.status === \"Ready\" &&\n fields.prodCtx?.status === \"Ready\" &&\n fields.prodUiCtx?.status === \"Ready\"\n ) {\n this.setBlockFieldObj(blockId, \"prodOutputPrevious\", fields.prodOutput);\n this.setBlockFieldObj(blockId, \"prodCtxPrevious\", fields.prodCtx);\n this.setBlockFieldObj(blockId, \"prodUiCtxPrevious\", fields.prodUiCtx);\n }\n this.deleteBlockFields(blockId, \"prodOutput\", \"prodCtx\", \"prodUiCtx\", \"prodArgs\");\n }\n\n /** Running blocks are reset, already computed moved to limbo. Returns if\n * either of the actions were actually performed.\n * This method ensures the block is left in a consistent state that passes check() constraints. */\n private resetOrLimboProduction(blockId: string): boolean {\n const fields = this.getBlockInfo(blockId).fields;\n\n // Check if we can safely move to limbo (both core production fields are ready)\n if (fields.prodOutput?.status === \"Ready\" && fields.prodCtx?.status === \"Ready\") {\n if (this.blocksInLimbo.has(blockId))\n // we are already in limbo\n return false;\n\n // limbo - keep the ready production results but clean up cache\n this.blocksInLimbo.add(blockId);\n this.renderingStateChanged = true;\n\n // doing some gc - clean up previous cache fields\n this.deleteBlockFields(blockId, \"prodOutputPrevious\", \"prodCtxPrevious\", \"prodUiCtxPrevious\");\n\n return true;\n } else {\n // reset - clean up any partial/inconsistent production stat\n return this.deleteBlockFields(\n blockId,\n \"prodOutput\",\n \"prodCtx\",\n \"prodUiCtx\",\n \"prodArgs\",\n \"prodOutputPrevious\",\n \"prodCtxPrevious\",\n \"prodUiCtxPrevious\",\n );\n }\n }\n\n /**\n * Gets current block state and merges with partial updates.\n * Used by legacy v1/v2 methods like setBlockArgs and setUiState.\n *\n * @param blockId The block to get state for\n * @param partialUpdate Partial state to merge (e.g. { args } or { uiState })\n * @returns Merged state in unified format { args, uiState }\n */\n public mergeBlockState(\n blockId: string,\n partialUpdate: { args?: unknown; uiState?: unknown },\n ): { args?: unknown; uiState?: unknown } {\n const info = this.getBlockInfo(blockId);\n const currentState = info.blockStorage as { args?: unknown; uiState?: unknown } | undefined;\n if (currentState === undefined) {\n throw new Error(`Cannot merge block state for ${blockId}: blockStorage is unavailable`);\n }\n return { ...currentState, ...partialUpdate };\n }\n\n /**\n * Sets raw block storage content directly (for testing purposes).\n * This bypasses all normalization and VM transformations.\n *\n * @param blockId The block to set storage for\n * @param rawStorageJson Raw storage as JSON string\n */\n public setBlockStorageRaw(blockId: string, rawStorageJson: string): void {\n this.setBlockFieldObj(\n blockId,\n \"blockStorage\",\n this.createJsonFieldValueByContent(rawStorageJson),\n );\n this.blocksWithChangedInputs.add(blockId);\n this.updateLastModified();\n }\n\n /**\n * Resets a v2+ block to its initial storage state.\n * Gets initial storage from VM and derives args from it.\n *\n * For v1 blocks, use setStates() instead.\n *\n * @param blockId The block to reset\n */\n public resetToInitialStorage(blockId: string): void {\n const info = this.getBlockInfo(blockId);\n const blockConfig = info.config;\n\n if (blockConfig.modelAPIVersion !== BLOCK_STORAGE_FACADE_VERSION) {\n throw new Error(\"resetToInitialStorage is only supported for model API version 2\");\n }\n\n // Get initial storage from VM\n const initialStorageJson = this.projectHelper.getInitialStorageInVM(blockConfig);\n this.setBlockStorageRaw(blockId, initialStorageJson);\n\n // Derive args from storage - set or clear currentArgs based on derivation result\n const deriveArgsResult = this.projectHelper.deriveArgsFromStorage(\n blockConfig,\n initialStorageJson,\n );\n if (!deriveArgsResult.error) {\n this.setBlockFieldObj(\n blockId,\n \"currentArgs\",\n this.createJsonFieldValue(deriveArgsResult.value),\n );\n // Derive prerunArgs from storage\n const prerunArgs = this.projectHelper.derivePrerunArgsFromStorage(\n blockConfig,\n initialStorageJson,\n );\n if (prerunArgs !== undefined) {\n this.setBlockFieldObj(blockId, \"currentPrerunArgs\", this.createJsonFieldValue(prerunArgs));\n } else {\n this.deleteBlockFields(blockId, \"currentPrerunArgs\");\n }\n } else {\n if (info.fields.currentPrerunArgs !== undefined) {\n this.projectHelper.logger.warn(\n `[staging] ${blockId}: currentPrerunArgs cleared (args derivation failed)`,\n );\n }\n this.deleteBlockFields(blockId, \"currentArgs\");\n this.deleteBlockFields(blockId, \"currentPrerunArgs\");\n }\n }\n\n /** Optimally sets inputs for multiple blocks in one go */\n public setStates(requests: SetStatesRequest[]) {\n const changedArgs: string[] = [];\n let somethingChanged = false;\n for (const req of requests) {\n const info = this.getBlockInfo(req.blockId);\n let blockChanged = false;\n\n const blockConfig = info.config;\n // modelAPIVersion === 2 means BlockModelV3 with .args() lambda for deriving args\n\n if (req.modelAPIVersion !== blockConfig.modelAPIVersion) {\n throw new Error(\n `Model API version mismatch for block ${req.blockId}: ${req.modelAPIVersion} !== ${blockConfig.modelAPIVersion}`,\n );\n }\n\n // Derive args from storage using the block's config.args() callback\n let args: unknown;\n let prerunArgs: unknown;\n\n if (req.modelAPIVersion === BLOCK_STORAGE_FACADE_VERSION) {\n const currentStorageJson = info.blockStorageJson;\n if (currentStorageJson === undefined) {\n throw new Error(`Block ${req.blockId} has no blockStorage - this should not happen`);\n }\n\n // Apply the state update to storage\n const updatedStorageJson = this.projectHelper.applyStorageUpdateInVM(\n blockConfig,\n currentStorageJson,\n req.payload,\n );\n\n this.setBlockFieldObj(\n req.blockId,\n \"blockStorage\",\n this.createJsonFieldValueByContent(updatedStorageJson),\n );\n\n // Derive args directly from storage (VM extracts data internally)\n const derivedArgsResult = this.projectHelper.deriveArgsFromStorage(\n blockConfig,\n updatedStorageJson,\n );\n if (derivedArgsResult.error) {\n args = undefined;\n prerunArgs = undefined;\n } else {\n args = derivedArgsResult.value;\n // Derive prerunArgs from storage, or fall back to args\n prerunArgs = this.projectHelper.derivePrerunArgsFromStorage(\n blockConfig,\n updatedStorageJson,\n );\n }\n } else {\n this.setBlockFieldObj(req.blockId, \"blockStorage\", this.createJsonFieldValue(req.state));\n if (req.state !== null && typeof req.state === \"object\" && \"args\" in req.state) {\n args = (req.state as { args: unknown }).args;\n } else {\n args = req.state;\n }\n // For the legacy blocks, prerunArgs = args (same as production args)\n prerunArgs = args;\n }\n\n // Set or clear currentArgs based on derivation result\n if (args !== undefined) {\n const currentArgsData = canonicalJsonBytes(args);\n const argsPartRef = this.tx.createValue(Pl.JsonObject, currentArgsData);\n this.setBlockField(req.blockId, \"currentArgs\", argsPartRef, \"Ready\", currentArgsData);\n } else {\n this.deleteBlockFields(req.blockId, \"currentArgs\");\n }\n\n // Set currentPrerunArgs field and check if it actually changed\n let prerunArgsChanged = false;\n if (prerunArgs !== undefined) {\n const prerunArgsData = canonicalJsonBytes(prerunArgs);\n const oldPrerunArgsData = info.fields.currentPrerunArgs?.value;\n // Check if prerunArgs actually changed\n if (\n oldPrerunArgsData === undefined ||\n Buffer.compare(oldPrerunArgsData, prerunArgsData) !== 0\n ) {\n prerunArgsChanged = true;\n }\n const prerunArgsRef = this.tx.createValue(Pl.JsonObject, prerunArgsData);\n this.setBlockField(\n req.blockId,\n \"currentPrerunArgs\",\n prerunArgsRef,\n \"Ready\",\n prerunArgsData,\n );\n } else {\n this.deleteBlockFields(req.blockId, \"currentPrerunArgs\");\n }\n\n blockChanged = true;\n // Only add to changedArgs if prerunArgs changed - this controls staging reset\n if (prerunArgsChanged) {\n changedArgs.push(req.blockId);\n }\n\n if (blockChanged) {\n // will be assigned our author marker\n this.blocksWithChangedInputs.add(req.blockId);\n somethingChanged = true;\n }\n }\n\n // resetting staging outputs for all downstream blocks\n this.getStagingGraph().traverse(\"downstream\", changedArgs, ({ id }) => this.resetStaging(id));\n\n if (somethingChanged) this.updateLastModified();\n }\n\n public setBlockSettings(blockId: string, newValue: BlockSettings): void {\n this.setBlockFieldObj(blockId, \"blockSettings\", this.createJsonFieldValue(newValue));\n this.updateLastModified();\n }\n\n private createProdCtx(upstream: Set<string>): AnyRef {\n const upstreamContexts: AnyRef[] = [];\n upstream.forEach((id) => {\n const info = this.getBlockInfo(id);\n if (info.fields[\"prodCtx\"]?.ref === undefined)\n throw new Error(\"One of the upstreams staging is not rendered.\");\n upstreamContexts.push(Pl.unwrapHolder(this.tx, info.fields[\"prodCtx\"].ref));\n });\n return createBContextFromUpstreams(this.tx, upstreamContexts);\n }\n\n private createStagingCtx(upstream: Set<string>): AnyRef {\n const upstreamContexts: AnyRef[] = [];\n upstream.forEach((id) => {\n const info = this.getBlockInfo(id);\n if (info.fields[\"stagingCtx\"]?.ref !== undefined) {\n upstreamContexts.push(Pl.unwrapHolder(this.tx, info.fields[\"stagingCtx\"].ref));\n } else if (info.fields.currentPrerunArgs !== undefined) {\n // Upstream has currentPrerunArgs but no staging — this is an inconsistency\n throw new Error(`Upstream ${id} staging is not rendered but has currentPrerunArgs set.`);\n }\n // Blocks without currentPrerunArgs (e.g. block model doesn't define prerunArgs, or args\n // derivation failed) never get stagingCtx. Use prodCtx if available.\n if (info.fields[\"prodCtx\"]?.ref !== undefined)\n upstreamContexts.push(Pl.unwrapHolder(this.tx, info.fields[\"prodCtx\"].ref));\n });\n return createBContextFromUpstreams(this.tx, upstreamContexts);\n }\n\n private exportCtx(ctx: AnyRef): AnyRef {\n return exportContext(this.tx, Pl.unwrapHolder(this.tx, this.ctxExportTplHolder), ctx);\n }\n\n /**\n * Renders staging for a block using currentPrerunArgs.\n * If currentPrerunArgs is not set (prerunArgs returned undefined), skips staging for this block.\n */\n private renderStagingFor(blockId: string) {\n const info = this.getBlockInfo(blockId);\n\n // Check BEFORE resetStaging: if currentPrerunArgs is not set (e.g. prerunArgs() returned undefined\n // because inputs aren't ready, or args derivation failed), skip without clearing existing staging.\n // Otherwise resetStaging would delete stagingCtx, and downstream blocks that reference this block\n // as an upstream would fail in createStagingCtx.\n const prerunArgsRef = info.fields.currentPrerunArgs?.ref;\n if (prerunArgsRef === undefined) {\n return;\n }\n\n this.resetStaging(blockId);\n\n const ctx = this.createStagingCtx(this.getStagingGraph().nodes.get(blockId)!.upstream);\n\n if (this.getBlock(blockId).renderingMode !== \"Heavy\") throw new Error(\"not supported yet\");\n\n const tpl = info.getTemplate(this.tx);\n\n // Use currentPrerunArgs for staging rendering\n const results = createRenderHeavyBlock(this.tx, tpl, {\n args: prerunArgsRef,\n blockId: this.tx.createValue(Pl.JsonString, JSON.stringify(blockId)),\n isProduction: this.tx.createValue(Pl.JsonBool, JSON.stringify(false)),\n context: ctx,\n });\n\n // Here we set the staging ctx to the input context of the staging workflow, not the output because exports\n // of one staging context should stay within the same block, and not travel downstream.\n // We may change this decision in the future if wanted to support traveling staging exports downstream.\n this.setBlockField(blockId, \"stagingCtx\", Pl.wrapInEphHolder(this.tx, ctx), \"NotReady\");\n\n // Yet the staging UI Ctx is the output context of the staging workflow, because it is used to form the result pool\n // thus creating a certain discrepancy between staging workflow context behavior and desktop's result pool.\n this.setBlockField(blockId, \"stagingUiCtx\", this.exportCtx(results.context), \"NotReady\");\n this.setBlockField(blockId, \"stagingOutput\", results.result, \"NotReady\");\n }\n\n private renderProductionFor(blockId: string) {\n this.resetProduction(blockId);\n\n const info = this.getBlockInfo(blockId);\n\n // Can't render production if currentArgs is not set\n if (info.fields.currentArgs === undefined) {\n throw new Error(`Can't render production for block ${blockId}: currentArgs not set`);\n }\n\n const ctx = this.createProdCtx(this.getPendingProductionGraph().nodes.get(blockId)!.upstream);\n\n if (this.getBlock(blockId).renderingMode === \"Light\")\n throw new Error(\"Can't render production for light block.\");\n\n const tpl = info.getTemplate(this.tx);\n\n const results = createRenderHeavyBlock(this.tx, tpl, {\n args: info.fields.currentArgs.ref!,\n blockId: this.tx.createValue(Pl.JsonString, JSON.stringify(blockId)),\n isProduction: this.tx.createValue(Pl.JsonBool, JSON.stringify(true)),\n context: ctx,\n });\n this.setBlockField(\n blockId,\n \"prodCtx\",\n Pl.wrapInEphHolder(this.tx, results.context),\n \"NotReady\",\n );\n this.setBlockField(blockId, \"prodUiCtx\", this.exportCtx(results.context), \"NotReady\");\n this.setBlockField(blockId, \"prodOutput\", results.result, \"NotReady\");\n\n // saving inputs for which we rendered the production\n this.setBlockFieldObj(blockId, \"prodArgs\", info.fields.currentArgs);\n\n // removing block from limbo as we juts rendered fresh production for it\n if (this.blocksInLimbo.delete(blockId)) this.renderingStateChanged = true;\n }\n\n //\n // Structure changes\n //\n\n private initializeNewBlock(blockId: string, spec: NewBlockSpec): void {\n const info = new BlockInfo(\n blockId,\n {},\n extractConfig(spec.blockPack.config),\n spec.blockPack.source,\n this.projectHelper.logger,\n );\n this.blockInfos.set(blockId, info);\n\n // block pack\n const bp = createBlockPack(this.tx, spec.blockPack);\n this.setBlockField(blockId, \"blockPack\", Pl.wrapInHolder(this.tx, bp), \"NotReady\");\n\n // settings\n this.setBlockFieldObj(\n blockId,\n \"blockSettings\",\n this.createJsonFieldValue(InitialBlockSettings),\n );\n\n const blockConfig = info.config;\n\n let args: unknown;\n let prerunArgs: unknown;\n let storageToWrite: string;\n\n if (spec.storageMode === \"fromModel\") {\n // Model API v2+: get initial storage and derive args from it\n storageToWrite = this.projectHelper.getInitialStorageInVM(blockConfig);\n\n // Derive args directly from storage (VM extracts data internally)\n const deriveArgsResult = this.projectHelper.deriveArgsFromStorage(\n blockConfig,\n storageToWrite,\n );\n if (deriveArgsResult.error) {\n args = undefined;\n prerunArgs = undefined;\n } else {\n args = deriveArgsResult.value;\n prerunArgs = this.projectHelper.derivePrerunArgsFromStorage(blockConfig, storageToWrite);\n }\n } else if (spec.storageMode === \"legacy\") {\n // Model API v1: use legacyState from spec\n const parsedState = JSON.parse(spec.legacyState);\n args = parsedState.args;\n if (args === undefined) {\n throw new Error(\"args is undefined in legacyState\");\n }\n prerunArgs = args;\n storageToWrite = spec.legacyState;\n } else {\n throw new Error(`Unknown storageMode: ${(spec as NewBlockSpec).storageMode}`);\n }\n\n // currentArgs\n if (args !== undefined) {\n this.setBlockFieldObj(blockId, \"currentArgs\", this.createJsonFieldValue(args));\n }\n\n // currentPrerunArgs\n if (prerunArgs !== undefined) {\n this.setBlockFieldObj(blockId, \"currentPrerunArgs\", this.createJsonFieldValue(prerunArgs));\n }\n\n // blockStorage\n this.setBlockFieldObj(\n blockId,\n \"blockStorage\",\n this.createJsonFieldValueByContent(storageToWrite),\n );\n\n // checking structure\n info.check();\n }\n\n private getFieldNamesToDuplicate(blockId: string): Set<ProjectField[\"fieldName\"]> {\n const fields = this.getBlockInfo(blockId).fields;\n\n const diff = <T>(setA: Set<T>, setB: Set<T>): Set<T> =>\n new Set([...setA].filter((x) => !setB.has(x)));\n\n // Check if we can safely move to limbo (both core production fields are ready)\n if (fields.prodOutput?.status === \"Ready\" && fields.prodCtx?.status === \"Ready\") {\n if (this.blocksInLimbo.has(blockId))\n // we are already in limbo\n return FieldsToDuplicate;\n\n return diff(\n FieldsToDuplicate,\n new Set([\"prodOutputPrevious\", \"prodCtxPrevious\", \"prodUiCtxPrevious\"]),\n );\n } else {\n return diff(\n FieldsToDuplicate,\n new Set([\n \"prodOutput\",\n \"prodCtx\",\n \"prodUiCtx\",\n \"prodArgs\",\n \"prodOutputPrevious\",\n \"prodCtxPrevious\",\n \"prodUiCtxPrevious\",\n ]),\n );\n }\n }\n\n private initializeBlockDuplicate(blockId: string, originalBlockInfo: BlockInfo) {\n const info = new BlockInfo(\n blockId,\n {},\n originalBlockInfo.config,\n originalBlockInfo.source,\n this.projectHelper.logger,\n );\n\n this.blockInfos.set(blockId, info);\n\n const fieldNamesToDuplicate = this.getFieldNamesToDuplicate(blockId);\n\n // Copy all fields from original block to new block by sharing references\n for (const [fieldName, fieldState] of Object.entries(originalBlockInfo.fields)) {\n if (\n fieldNamesToDuplicate.has(fieldName as ProjectField[\"fieldName\"]) &&\n fieldState &&\n fieldState.ref\n ) {\n this.setBlockFieldObj(blockId, fieldName as keyof BlockFieldStates, {\n ref: fieldState.ref,\n status: fieldState.status,\n value: fieldState.value,\n });\n }\n }\n\n this.resetOrLimboProduction(blockId);\n\n info.check();\n }\n\n /** Very generic method, better check for more specialized case-specific methods first. */\n public updateStructure(\n newStructure: ProjectStructure,\n newBlockInitializer: (blockId: string) => void = NoNewBlocks,\n ): void {\n const currentStagingGraph = this.getStagingGraph();\n const currentActualProductionGraph = this.getActualProductionGraph();\n\n const newStagingGraph = stagingGraph(newStructure);\n\n const stagingDiff = graphDiff(currentStagingGraph, newStagingGraph);\n\n // removing blocks\n for (const blockId of stagingDiff.onlyInA) {\n const { fields } = this.getBlockInfo(blockId);\n this.deleteBlockFields(blockId, ...(Object.keys(fields) as ProjectField[\"fieldName\"][]));\n this.blockInfos.delete(blockId);\n if (this.blocksInLimbo.delete(blockId)) this.renderingStateChanged = true;\n }\n\n // creating new blocks\n for (const blockId of stagingDiff.onlyInB) {\n newBlockInitializer(blockId);\n }\n\n // resetting stagings affected by topology change\n for (const blockId of stagingDiff.different) this.resetStaging(blockId);\n\n // new actual production graph without new blocks\n const newActualProductionGraph = productionGraph(newStructure, (blockId) =>\n this.getProductionGraphBlockInfo(blockId, true),\n );\n\n const prodDiff = graphDiff(currentActualProductionGraph, newActualProductionGraph);\n\n // applying changes due to topology change in production to affected nodes and\n // all their downstreams\n currentActualProductionGraph.traverse(\"downstream\", [...prodDiff.different], (node) => {\n this.resetOrLimboProduction(node.id);\n });\n\n if (\n stagingDiff.onlyInB.size > 0 ||\n stagingDiff.onlyInA.size > 0 ||\n stagingDiff.different.size > 0\n )\n this.resetStagingRefreshTimestamp();\n\n this.struct = newStructure;\n this.structureChanged = true;\n this.stagingGraph = undefined;\n this.pendingProductionGraph = undefined;\n this.actualProductionGraph = undefined;\n\n this.updateLastModified();\n }\n\n //\n // Structure change helpers\n //\n\n public addBlock(block: Block, spec: NewBlockSpec, before?: string): void {\n const newStruct = this.structure; // copy current structure\n if (before === undefined) {\n // adding as a very last block\n newStruct.groups[newStruct.groups.length - 1].blocks.push(block);\n } else {\n let done = false;\n for (const group of newStruct.groups) {\n const idx = group.blocks.findIndex((b) => b.id === before);\n if (idx < 0) continue;\n group.blocks.splice(idx, 0, block);\n done = true;\n break;\n }\n if (!done) throw new Error(`Can't find element with id: ${before}`);\n }\n this.updateStructure(newStruct, (blockId) => {\n if (blockId !== block.id) throw new Error(\"Unexpected\");\n this.initializeNewBlock(blockId, spec);\n });\n }\n\n /**\n * Duplicates an existing block by copying all its fields and structure.\n * This method creates a deep copy of the block at the mutator level.\n *\n * @param originalBlockId id of the block to duplicate\n * @param newBlockId id for the new duplicated block\n * @param after id of the block to insert new block after\n */\n public duplicateBlock(originalBlockId: string, newBlockId: string, after?: string): void {\n // Get the original block from structure\n const originalBlock = this.getBlock(originalBlockId);\n const originalBlockInfo = this.getBlockInfo(originalBlockId);\n\n // Create new block in structure\n const newBlock: Block = {\n id: newBlockId,\n label: originalBlock.label,\n renderingMode: originalBlock.renderingMode,\n };\n\n // Add the new block to structure\n const newStruct = this.structure; // copy current structure\n if (after === undefined) {\n // adding as a very last block\n newStruct.groups[newStruct.groups.length - 1].blocks.push(newBlock);\n } else {\n let done = false;\n for (const group of newStruct.groups) {\n const idx = group.blocks.findIndex((b) => b.id === after);\n if (idx < 0) continue;\n group.blocks.splice(idx + 1, 0, newBlock);\n done = true;\n break;\n }\n if (!done) throw new Error(`Can't find element with id: ${after}`);\n }\n\n this.updateStructure(newStruct, (blockId) => {\n if (blockId !== newBlockId) throw new Error(\"Unexpected\");\n this.initializeBlockDuplicate(blockId, originalBlockInfo);\n });\n }\n\n public deleteBlock(blockId: string): void {\n const newStruct = this.structure; // copy current structure\n let done = false;\n for (const group of newStruct.groups) {\n const idx = group.blocks.findIndex((b) => b.id === blockId);\n if (idx < 0) continue;\n group.blocks.splice(idx, 1);\n done = true;\n break;\n }\n if (!done) throw new Error(`Can't find element with id: ${blockId}`);\n this.updateStructure(newStruct);\n }\n\n //\n // Block-pack migration\n //\n\n public migrateBlockPack(\n blockId: string,\n spec: BlockPackSpecPrepared,\n newClearState?: ClearState,\n ): void {\n const info = this.getBlockInfo(blockId);\n const newConfig = extractConfig(spec.config);\n\n const persistBlockPack = () => {\n this.setBlockField(\n blockId,\n \"blockPack\",\n Pl.wrapInHolder(this.tx, createBlockPack(this.tx, spec)),\n \"NotReady\",\n );\n };\n\n const applyStorageAndDeriveArgs = (storageJson: string) => {\n persistBlockPack();\n this.setBlockStorageRaw(blockId, storageJson);\n const deriveArgsResult = this.projectHelper.deriveArgsFromStorage(newConfig, storageJson);\n if (!deriveArgsResult.error) {\n this.setBlockFieldObj(\n blockId,\n \"currentArgs\",\n this.createJsonFieldValue(deriveArgsResult.value),\n );\n const prerunArgs = this.projectHelper.derivePrerunArgsFromStorage(newConfig, storageJson);\n if (prerunArgs !== undefined) {\n this.setBlockFieldObj(\n blockId,\n \"currentPrerunArgs\",\n this.createJsonFieldValue(prerunArgs),\n );\n } else {\n this.deleteBlockFields(blockId, \"currentPrerunArgs\");\n }\n } else {\n this.deleteBlockFields(blockId, \"currentArgs\");\n this.deleteBlockFields(blockId, \"currentPrerunArgs\");\n }\n };\n\n if (newClearState !== undefined) {\n // State is being reset - no migration needed\n const supportsStorageFromVM = newConfig.modelAPIVersion === BLOCK_STORAGE_FACADE_VERSION;\n\n if (supportsStorageFromVM) {\n // V2+: Get initial storage directly from VM and derive args from it\n const initialStorageJson = this.projectHelper.getInitialStorageInVM(newConfig);\n applyStorageAndDeriveArgs(initialStorageJson);\n this.blocksWithChangedInputs.add(blockId);\n this.updateLastModified();\n } else {\n // V1: Use setStates with legacy state format\n persistBlockPack();\n this.setStates([{ modelAPIVersion: 1, blockId, state: newClearState.state }]);\n }\n } else {\n // State is being preserved - run migrations if needed via VM\n // Only Model API v2 blocks support migrations\n const supportsStateMigrations = newConfig.modelAPIVersion === BLOCK_STORAGE_FACADE_VERSION;\n\n if (supportsStateMigrations) {\n const currentStorageJson = info.blockStorageJson;\n\n // Attempt migration BEFORE persisting block pack — on failure,\n // block stays on old version (no inconsistent new-code/old-storage state)\n const migrationResult = this.projectHelper.migrateStorageInVM(\n newConfig,\n currentStorageJson,\n );\n\n if (migrationResult.error !== undefined) {\n throw new Error(\n `[migrateBlockPack] Block ${blockId} migration failed: ${migrationResult.error}`,\n );\n }\n\n this.projectHelper.logger.info(\n `[migrateBlockPack] Block ${blockId}: ${migrationResult.info}`,\n );\n applyStorageAndDeriveArgs(migrationResult.newStorageJson);\n } else {\n // Legacy blocks (modelAPIVersion 1): persist block pack, set prerunArgs = currentArgs\n persistBlockPack();\n if (info.fields.currentArgs !== undefined) {\n this.setBlockFieldObj(blockId, \"currentPrerunArgs\", info.fields.currentArgs);\n }\n }\n\n this.blocksWithChangedInputs.add(blockId);\n\n // resetting staging outputs for all downstream blocks\n this.getStagingGraph().traverse(\"downstream\", [blockId], ({ id }) => this.resetStaging(id));\n }\n\n // also reset or limbo all downstream productions\n if (info.productionRendered)\n this.getActualProductionGraph().traverse(\"downstream\", [blockId], ({ id }) =>\n this.resetOrLimboProduction(id),\n );\n\n this.updateLastModified();\n }\n\n //\n // Render\n //\n\n public renderProduction(blockIds: string[], addUpstreams: boolean = false): Set<string> {\n const blockIdsSet = new Set(blockIds);\n\n const prodGraph = this.getPendingProductionGraph();\n if (addUpstreams)\n // adding all upstreams automatically\n prodGraph.traverse(\"upstream\", blockIds, (node) => {\n blockIdsSet.add(node.id);\n });\n else\n // checking that targets contain all upstreams\n for (const blockId of blockIdsSet) {\n const node = prodGraph.nodes.get(blockId);\n if (node === undefined) throw new Error(`Can't find block with id: ${blockId}`);\n for (const upstream of node.upstream)\n if (!blockIdsSet.has(upstream))\n throw new Error(\"Can't render blocks not including all upstreams.\");\n }\n\n // traversing in topological order and rendering target blocks\n const rendered = new Set<string>();\n for (const block of allBlocks(this.structure)) {\n if (!blockIdsSet.has(block.id)) continue;\n\n let render =\n this.getBlockInfo(block.id).requireProductionRendering || this.blocksInLimbo.has(block.id);\n\n if (!render)\n for (const upstream of prodGraph.nodes.get(block.id)!.upstream)\n if (rendered.has(upstream)) {\n render = true;\n break;\n }\n\n if (render) {\n this.renderProductionFor(block.id);\n rendered.add(block.id);\n }\n }\n\n const renderedArray = [...rendered];\n\n // sending to limbo all downstream blocks\n prodGraph.traverse(\"downstream\", renderedArray, (node) => {\n if (rendered.has(node.id))\n // don't send to limbo blocks that were just rendered\n return;\n this.resetOrLimboProduction(node.id);\n });\n\n // resetting staging outputs for all downstream blocks\n this.getStagingGraph().traverse(\"downstream\", renderedArray, ({ id }) => {\n // don't reset staging of the first rendered block\n if (renderedArray[0] !== id) this.resetStaging(id);\n });\n\n if (rendered.size > 0) this.updateLastModified();\n\n return rendered;\n }\n\n /** Stops running blocks from the list and modify states of other blocks\n * accordingly */\n public stopProduction(...blockIds: string[]) {\n const activeProdGraph = this.getActualProductionGraph();\n\n // we will stop all blocks listed in request and all their downstreams\n const queue = new Denque(blockIds);\n const queued = new Set(blockIds);\n const stopped: string[] = [];\n\n while (!queue.isEmpty()) {\n const blockId = queue.shift()!;\n const fields = this.getBlockInfo(blockId).fields;\n\n if (fields.prodOutput?.status === \"Ready\" && fields.prodCtx?.status === \"Ready\")\n // skipping finished blocks\n continue;\n\n if (this.deleteBlockFields(blockId, \"prodOutput\", \"prodCtx\", \"prodUiCtx\", \"prodArgs\")) {\n // was actually stopped\n stopped.push(blockId);\n\n // will try to stop all its downstreams\n for (const downstream of activeProdGraph.traverseIdsExcludingRoots(\"downstream\", blockId)) {\n if (queued.has(downstream)) continue;\n queue.push(downstream);\n queued.add(downstream);\n }\n }\n }\n\n // blocks under stopped blocks, but having finished production results, goes to limbo\n for (const blockId of activeProdGraph.traverseIdsExcludingRoots(\"downstream\", ...stopped))\n this.resetOrLimboProduction(blockId);\n\n // reset staging outputs for all downstream blocks\n this.getStagingGraph().traverse(\"downstream\", stopped, ({ id }) => this.resetStaging(id));\n }\n\n private traverseWithStagingLag(cb: (blockId: string, lag: number) => void) {\n const lags = new Map<string, number>();\n const stagingGraph = this.getStagingGraph();\n stagingGraph.nodes.forEach((node) => {\n const info = this.getBlockInfo(node.id);\n // Use requireStagingRendering to check both: staging exists AND prerunArgs hasn't changed\n const requiresRendering = info.requireStagingRendering;\n let lag = requiresRendering ? 1 : 0;\n node.upstream.forEach((upstream) => {\n const upstreamLag = lags.get(upstream)!;\n if (upstreamLag === 0) return;\n lag = Math.max(upstreamLag + 1, lag);\n });\n if (!requiresRendering && info.stagingRendered) {\n // console.log(`[traverseWithStagingLag] SKIP staging for ${node.id} - prerunArgs unchanged`);\n }\n cb(node.id, lag);\n lags.set(node.id, lag);\n });\n }\n\n /** @param stagingRenderingRate rate in blocks per second */\n private refreshStagings(stagingRenderingRate?: number) {\n const elapsed = Date.now() - this.renderingState.stagingRefreshTimestamp;\n const lagThreshold =\n stagingRenderingRate === undefined\n ? undefined\n : 1 + Math.max(0, (elapsed * stagingRenderingRate) / 1000);\n let rendered = 0;\n this.traverseWithStagingLag((blockId, lag) => {\n if (lag === 0)\n // meaning staging already rendered\n return;\n if (lagThreshold === undefined || lag <= lagThreshold) {\n try {\n this.renderStagingFor(blockId);\n rendered++;\n } catch (e) {\n this.projectHelper.logger.error(\n new Error(`[refreshStagings] renderStagingFor failed for ${blockId}`, { cause: e }),\n );\n }\n }\n });\n if (rendered > 0) this.resetStagingRefreshTimestamp();\n }\n\n //\n // Meta\n //\n\n /** Updates project metadata */\n public setMeta(meta: ProjectMeta): void {\n this.meta = meta;\n this.metaChanged = true;\n this.updateLastModified();\n }\n\n //\n // Maintenance\n //\n\n /** @param stagingRenderingRate rate in blocks per second */\n public doRefresh(stagingRenderingRate?: number) {\n this.refreshStagings(stagingRenderingRate);\n this.blockInfos.forEach((blockInfo) => {\n if (\n blockInfo.fields.prodCtx?.status === \"Ready\" &&\n blockInfo.fields.prodOutput?.status === \"Ready\"\n )\n this.deleteBlockFields(\n blockInfo.id,\n \"prodOutputPrevious\",\n \"prodCtxPrevious\",\n \"prodUiCtxPrevious\",\n );\n if (\n blockInfo.fields.stagingCtx?.status === \"Ready\" &&\n blockInfo.fields.stagingOutput?.status === \"Ready\"\n )\n this.deleteBlockFields(\n blockInfo.id,\n \"stagingOutputPrevious\",\n \"stagingCtxPrevious\",\n \"stagingUiCtxPrevious\",\n );\n });\n }\n\n private assignAuthorMarkers() {\n const markerStr = this.author ? JSON.stringify(this.author) : undefined;\n\n for (const blockId of this.blocksWithChangedInputs)\n if (markerStr === undefined) this.tx.deleteKValue(this.rid, blockArgsAuthorKey(blockId));\n else this.tx.setKValue(this.rid, blockArgsAuthorKey(blockId), markerStr);\n\n if (this.metaChanged || this.structureChanged) {\n if (markerStr === undefined) this.tx.deleteKValue(this.rid, ProjectStructureAuthorKey);\n else this.tx.setKValue(this.rid, ProjectStructureAuthorKey, markerStr);\n }\n }\n\n public save() {\n if (!this.wasModified) return;\n\n if (this.lastModifiedChanged)\n this.tx.setKValue(this.rid, ProjectLastModifiedTimestamp, JSON.stringify(this.lastModified));\n\n if (this.structureChanged)\n this.tx.setKValue(this.rid, ProjectStructureKey, JSON.stringify(this.struct));\n\n if (this.renderingStateChanged)\n this.tx.setKValue(\n this.rid,\n BlockRenderingStateKey,\n JSON.stringify({\n ...this.renderingState,\n blocksInLimbo: [...this.blocksInLimbo],\n } as ProjectRenderingState),\n );\n\n if (this.metaChanged) this.tx.setKValue(this.rid, ProjectMetaKey, JSON.stringify(this.meta));\n\n this.assignAuthorMarkers();\n }\n\n public static async load(\n projectHelper: ProjectHelper,\n tx: PlTransaction,\n rid: ResourceId,\n author?: AuthorMarker,\n ): Promise<ProjectMutator> {\n //\n // Sending initial requests to read project state (start of round-trip #1)\n //\n\n const fullResourceStateP = tx.getResourceData(rid, true);\n const schemaP = tx.getKValueJson<string>(rid, SchemaVersionKey);\n const lastModifiedP = tx.getKValueJson<number>(rid, ProjectLastModifiedTimestamp);\n const metaP = tx.getKValueJson<ProjectMeta>(rid, ProjectMetaKey);\n const structureP = tx.getKValueJson<ProjectStructure>(rid, ProjectStructureKey);\n const renderingStateP = tx.getKValueJson<ProjectRenderingState>(rid, BlockRenderingStateKey);\n\n const fullResourceState = await fullResourceStateP;\n\n // loading field information\n const blockInfoStates = new Map<string, BlockInfoState>();\n for (const f of fullResourceState.fields) {\n const projectField = parseProjectField(f.name);\n\n // processing only fields with known structure\n if (projectField === undefined) continue;\n\n let info = blockInfoStates.get(projectField.blockId);\n if (info === undefined) {\n info = {\n id: projectField.blockId,\n fields: {},\n };\n blockInfoStates.set(projectField.blockId, info);\n }\n\n info.fields[projectField.fieldName] = isNullResourceId(f.value)\n ? { modCount: 0 }\n : { modCount: 0, ref: f.value };\n }\n\n //\n // Roundtrip #1 not yet finished, but as soon as field list is received,\n // we can start sending requests to read states of referenced resources\n // (start of round-trip #2)\n //\n\n const blockFieldRequests: [\n BlockInfoState,\n ProjectField[\"fieldName\"],\n BlockFieldState,\n Promise<BasicResourceData | ResourceData>,\n ][] = [];\n blockInfoStates.forEach((info) => {\n const fields = info.fields;\n for (const [fName, state] of Object.entries(fields)) {\n if (state.ref === undefined) continue;\n if (!isResource(state.ref) || isResourceRef(state.ref))\n throw new Error(\"unexpected behaviour\");\n const fieldName = fName as ProjectField[\"fieldName\"];\n blockFieldRequests.push([\n info,\n fieldName,\n state,\n tx.getResourceData(state.ref, fieldName == \"blockPack\"),\n ]);\n }\n });\n\n // loading jsons\n const [schema, lastModified, meta, structure, { stagingRefreshTimestamp, blocksInLimbo }] =\n await Promise.all([schemaP, lastModifiedP, metaP, structureP, renderingStateP]);\n\n // Checking schema version of the project\n if (schema !== SchemaVersionCurrent) {\n if (Number(schema) < Number(SchemaVersionCurrent))\n throw new UiError(\n `Can't perform this action on this project because it has older schema. Try (re)loading the project to update it.`,\n );\n else\n throw new UiError(\n `Can't perform this action on this project because it has newer schema. Upgrade your desktop app to the latest version.`,\n );\n }\n\n //\n // <- at this point we have all the responses from round-trip #1\n //\n\n //\n // Receiving responses from round-trip #2 and sending requests to read block pack descriptions\n // (start of round-trip #3)\n //\n\n const blockPackRequests: [BlockInfoState, Promise<BasicResourceData>][] = [];\n for (const [info, fieldName, state, response] of blockFieldRequests) {\n const result = await response;\n state.value = result.data;\n if (isNotNullResourceId(result.error)) state.status = \"Error\";\n else if (result.resourceReady || isNotNullResourceId(result.originalResourceId))\n state.status = \"Ready\";\n else state.status = \"NotReady\";\n\n // For block pack we need to traverse the ref field from the resource data\n if (fieldName === \"blockPack\") {\n const refField = (result as ResourceData).fields.find((f) => f.name === Pl.HolderRefField);\n if (refField === undefined) throw new Error(\"Block pack ref field is missing\");\n blockPackRequests.push([\n info,\n tx.getResourceData(ensureResourceIdNotNull(refField.value), false),\n ]);\n }\n }\n\n //\n // <- at this point we have all the responses from round-trip #2\n //\n\n for (const [info, response] of blockPackRequests) {\n const result = await response;\n const bpInfo = cachedDeserialize<BlockPackInfo>(notEmpty(result.data));\n info.blockConfig = extractConfig(bpInfo.config);\n info.blockPack = bpInfo.source;\n }\n\n //\n // <- at this point we have all the responses from round-trip #3\n //\n\n // loading ctx export template to check if we already have cached materialized template in our project\n const ctxExportTplEnvelope = await getPreparedExportTemplateEnvelope();\n\n // expected field name\n const ctxExportTplCacheFieldName = getServiceTemplateField(ctxExportTplEnvelope.hash);\n const ctxExportTplField = fullResourceState.fields.find(\n (f) => f.name === ctxExportTplCacheFieldName,\n );\n let ctxExportTplHolder: AnyResourceRef;\n if (ctxExportTplField !== undefined)\n ctxExportTplHolder = ensureResourceIdNotNull(ctxExportTplField.value);\n else {\n ctxExportTplHolder = Pl.wrapInHolder(tx, loadTemplate(tx, ctxExportTplEnvelope.spec));\n tx.createField(\n field(rid, getServiceTemplateField(ctxExportTplEnvelope.hash)),\n \"Dynamic\",\n ctxExportTplHolder,\n );\n }\n\n const renderingState = { stagingRefreshTimestamp };\n const blocksInLimboSet = new Set(blocksInLimbo);\n\n const blockInfos = new Map<string, BlockInfo>();\n blockInfoStates.forEach(({ id, fields, blockConfig, blockPack }) =>\n blockInfos.set(\n id,\n new BlockInfo(id, fields, notEmpty(blockConfig), notEmpty(blockPack), projectHelper.logger),\n ),\n );\n\n // check consistency of project state\n const blockInStruct = new Set<string>();\n for (const b of allBlocks(structure)) {\n if (!blockInfos.has(b.id))\n throw new Error(`Inconsistent project structure: no inputs for ${b.id}`);\n blockInStruct.add(b.id);\n }\n blockInfos.forEach((info) => {\n if (!blockInStruct.has(info.id))\n throw new Error(`Inconsistent project structure: no structure entry for ${info.id}`);\n });\n\n const prj = new ProjectMutator(\n rid,\n tx,\n author,\n schema,\n lastModified,\n meta,\n structure,\n renderingState,\n blocksInLimboSet,\n blockInfos,\n ctxExportTplHolder,\n projectHelper,\n );\n\n prj.fixProblemsAndMigrate();\n\n return prj;\n }\n}\n\nexport interface ProjectState {\n schema: string;\n structure: ProjectStructure;\n renderingState: Omit<ProjectRenderingState, \"blocksInLimbo\">;\n blocksInLimbo: Set<string>;\n blockInfos: Map<string, BlockInfo>;\n}\n\nexport async function createProject(\n tx: PlTransaction,\n meta: ProjectMeta = InitialBlockMeta,\n): Promise<AnyResourceRef> {\n const prj = tx.createEphemeral(ProjectResourceType);\n tx.lock(prj);\n const ts = String(Date.now());\n tx.setKValue(prj, SchemaVersionKey, JSON.stringify(SchemaVersionCurrent));\n tx.setKValue(prj, ProjectCreatedTimestamp, ts);\n tx.setKValue(prj, ProjectLastModifiedTimestamp, ts);\n tx.setKValue(prj, ProjectMetaKey, JSON.stringify(meta));\n tx.setKValue(prj, ProjectStructureKey, JSON.stringify(InitialBlockStructure));\n tx.setKValue(prj, BlockRenderingStateKey, JSON.stringify(InitialProjectRenderingState));\n const ctxExportTplEnvelope = await getPreparedExportTemplateEnvelope();\n tx.createField(\n field(prj, getServiceTemplateField(ctxExportTplEnvelope.hash)),\n \"Dynamic\",\n Pl.wrapInHolder(tx, loadTemplate(tx, ctxExportTplEnvelope.spec)),\n );\n return prj;\n}\n\nexport async function withProject<T>(\n projectHelper: ProjectHelper,\n txOrPl: PlTransaction | PlClient,\n rid: ResourceId,\n cb: (p: ProjectMutator) => T | Promise<T>,\n ops?: Partial<TxOps>,\n): Promise<T> {\n return withProjectAuthored(projectHelper, txOrPl, rid, undefined, cb, ops);\n}\n\nexport async function withProjectAuthored<T>(\n projectHelper: ProjectHelper,\n txOrPl: PlTransaction | PlClient,\n rid: ResourceId,\n author: AuthorMarker | undefined,\n cb: (p: ProjectMutator) => T | Promise<T>,\n ops: Partial<TxOps> = {},\n): Promise<T> {\n if (txOrPl instanceof PlClient) {\n return await txOrPl.withWriteTx(\n \"ProjectAction\" + (ops.name ? `: ${ops.name}` : \"\"),\n async (tx) => {\n const mut = await ProjectMutator.load(projectHelper, tx, rid, author);\n const result = await cb(mut);\n if (!mut.wasModified)\n // skipping save and commit altogether if no modifications were actually made\n return result;\n mut.save();\n await tx.commit();\n if (getDebugFlags().logProjectMutationStat) console.log(JSON.stringify(tx.stat));\n return result;\n },\n ops,\n );\n } else {\n const mut = await ProjectMutator.load(projectHelper, txOrPl, rid, author);\n const result = await cb(mut);\n mut.save();\n return result;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAiGA,SAAS,OAAiB,SAAsB,SAA2B;CACzE,IAAI,cAAc;CAClB,IAAI,YAA+B;CACnC,IAAI,QAAuB;AAC3B,cAAa;AACX,MAAI,CAAC,aAAa;AAChB,iBAAc;AACd,eAAY,SAAS;AACrB,WAAQ,SAAS;AACjB,UAAO;;EAET,MAAM,eAAe,SAAS;AAC9B,MAAI,cAAc,cAAc;AAC9B,eAAY;AACZ,WAAQ,SAAS;;AAEnB,SAAO,SAAS;;;AAIpB,IAAM,YAAN,MAAgB;CACd,YACE,AAAgB,IAChB,AAAgB,QAChB,AAAgB,QAChB,AAAgB,QAChB,AAAiB,SAAmB,IAAIA,iDAAsB,EAC9D;EALgB;EACA;EACA;EACA;EACC;;CAGnB,AAAO,QAAQ;AAGb,MAAK,KAAK,OAAO,eAAe,YAAgB,KAAK,OAAO,YAAY,QACtE,OAAM,IAAI,MAAM,2BAA2B;AAE7C,MAAK,KAAK,OAAO,kBAAkB,YAAgB,KAAK,OAAO,eAAe,QAC5E,OAAM,IAAI,MAAM,4BAA4B;AAE9C,MACG,KAAK,OAAO,uBAAuB,YACnC,KAAK,OAAO,oBAAoB,QAEjC,OAAM,IAAI,MAAM,iCAAiC;AAEnD,MACG,KAAK,OAAO,0BAA0B,YACtC,KAAK,OAAO,uBAAuB,QAEpC,OAAM,IAAI,MAAM,kCAAkC;AAEpD,MAAI,KAAK,OAAO,cAAc,OAAW,OAAM,IAAI,MAAM,sBAAsB;AAE/E,MAAI,KAAK,OAAO,iBAAiB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O;AACT,MAAI,MAAM;AACV,QAAM,GAAG,QAAQ;AACjB,MAAIC,6BAAe,CAAC,uBAAwB,SAAQ,IAAI,KAAK,UAAU,GAAG,KAAK,CAAC;AAChF,SAAO;IAET,IACD;MACI;EACL,MAAM,MAAM,MAAM,eAAe,KAAK,eAAe,QAAQ,KAAK,OAAO;EACzE,MAAM,SAAS,MAAM,GAAG,IAAI;AAC5B,MAAI,MAAM;AACV,SAAO"}
|
package/dist/mutator/project.js
CHANGED
|
@@ -142,7 +142,14 @@ var ProjectMutator = class ProjectMutator {
|
|
|
142
142
|
}
|
|
143
143
|
fixProblemsAndMigrate() {
|
|
144
144
|
this.blockInfos.forEach((blockInfo) => {
|
|
145
|
-
if (blockInfo.fields.prodArgs === void 0 || blockInfo.fields.prodOutput === void 0 || blockInfo.fields.prodCtx === void 0) this.deleteBlockFields(blockInfo.id, "prodArgs", "prodOutput", "prodCtx");
|
|
145
|
+
if (blockInfo.fields.prodArgs === void 0 || blockInfo.fields.prodOutput === void 0 || blockInfo.fields.prodCtx === void 0) this.deleteBlockFields(blockInfo.id, "prodArgs", "prodOutput", "prodCtx", "prodUiCtx");
|
|
146
|
+
});
|
|
147
|
+
this.blockInfos.forEach((blockInfo) => {
|
|
148
|
+
if (blockInfo.fields.stagingOutput === void 0 || blockInfo.fields.stagingCtx === void 0) this.deleteBlockFields(blockInfo.id, "stagingOutput", "stagingCtx", "stagingUiCtx");
|
|
149
|
+
});
|
|
150
|
+
this.blockInfos.forEach((blockInfo) => {
|
|
151
|
+
if (blockInfo.fields.prodOutputPrevious === void 0 || blockInfo.fields.prodCtxPrevious === void 0) this.deleteBlockFields(blockInfo.id, "prodOutputPrevious", "prodCtxPrevious", "prodUiCtxPrevious");
|
|
152
|
+
if (blockInfo.fields.stagingOutputPrevious === void 0 || blockInfo.fields.stagingCtxPrevious === void 0) this.deleteBlockFields(blockInfo.id, "stagingOutputPrevious", "stagingCtxPrevious", "stagingUiCtxPrevious");
|
|
146
153
|
});
|
|
147
154
|
let initialBlockSettings;
|
|
148
155
|
this.blockInfos.forEach((blockInfo) => {
|
|
@@ -151,6 +158,7 @@ var ProjectMutator = class ProjectMutator {
|
|
|
151
158
|
this.setBlockFieldObj(blockInfo.id, "blockSettings", initialBlockSettings);
|
|
152
159
|
}
|
|
153
160
|
});
|
|
161
|
+
this.blockInfos.forEach((info) => info.check());
|
|
154
162
|
}
|
|
155
163
|
get wasModified() {
|
|
156
164
|
return this.lastModifiedChanged || this.structureChanged || this.fieldsChanged || this.metaChanged || this.renderingStateChanged;
|
|
@@ -417,8 +425,8 @@ var ProjectMutator = class ProjectMutator {
|
|
|
417
425
|
const upstreamContexts = [];
|
|
418
426
|
upstream.forEach((id) => {
|
|
419
427
|
const info = this.getBlockInfo(id);
|
|
420
|
-
if (info.fields["stagingCtx"]?.ref
|
|
421
|
-
|
|
428
|
+
if (info.fields["stagingCtx"]?.ref !== void 0) upstreamContexts.push(Pl.unwrapHolder(this.tx, info.fields["stagingCtx"].ref));
|
|
429
|
+
else if (info.fields.currentPrerunArgs !== void 0) throw new Error(`Upstream ${id} staging is not rendered but has currentPrerunArgs set.`);
|
|
422
430
|
if (info.fields["prodCtx"]?.ref !== void 0) upstreamContexts.push(Pl.unwrapHolder(this.tx, info.fields["prodCtx"].ref));
|
|
423
431
|
});
|
|
424
432
|
return createBContextFromUpstreams(this.tx, upstreamContexts);
|
|
@@ -431,10 +439,10 @@ var ProjectMutator = class ProjectMutator {
|
|
|
431
439
|
* If currentPrerunArgs is not set (prerunArgs returned undefined), skips staging for this block.
|
|
432
440
|
*/
|
|
433
441
|
renderStagingFor(blockId) {
|
|
434
|
-
this.resetStaging(blockId);
|
|
435
442
|
const info = this.getBlockInfo(blockId);
|
|
436
443
|
const prerunArgsRef = info.fields.currentPrerunArgs?.ref;
|
|
437
444
|
if (prerunArgsRef === void 0) return;
|
|
445
|
+
this.resetStaging(blockId);
|
|
438
446
|
const ctx = this.createStagingCtx(this.getStagingGraph().nodes.get(blockId).upstream);
|
|
439
447
|
if (this.getBlock(blockId).renderingMode !== "Heavy") throw new Error("not supported yet");
|
|
440
448
|
const tpl = info.getTemplate(this.tx);
|
|
@@ -881,7 +889,6 @@ var ProjectMutator = class ProjectMutator {
|
|
|
881
889
|
}
|
|
882
890
|
blockInfos.forEach((info) => {
|
|
883
891
|
if (!blockInStruct.has(info.id)) throw new Error(`Inconsistent project structure: no structure entry for ${info.id}`);
|
|
884
|
-
info.check();
|
|
885
892
|
});
|
|
886
893
|
const prj = new ProjectMutator(rid, tx, author, schema, lastModified, meta, structure, renderingState, blocksInLimboSet, blockInfos, ctxExportTplHolder, projectHelper);
|
|
887
894
|
prj.fixProblemsAndMigrate();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"project.js","names":[],"sources":["../../src/mutator/project.ts"],"sourcesContent":["import type {\n AnyRef,\n AnyResourceRef,\n BasicResourceData,\n PlTransaction,\n ResourceData,\n ResourceId,\n TxOps,\n} from \"@milaboratories/pl-client\";\nimport {\n ensureResourceIdNotNull,\n field,\n isNotNullResourceId,\n isNullResourceId,\n isResource,\n isResourceId,\n isResourceRef,\n Pl,\n PlClient,\n} from \"@milaboratories/pl-client\";\nimport { createRenderHeavyBlock, createBContextFromUpstreams } from \"./template/render_block\";\nimport type {\n Block,\n ProjectStructure,\n ProjectField,\n ProjectRenderingState,\n} from \"../model/project_model\";\nimport {\n BlockRenderingStateKey,\n ProjectStructureKey,\n parseProjectField,\n projectFieldName,\n SchemaVersionCurrent,\n SchemaVersionKey,\n ProjectResourceType,\n InitialBlockStructure,\n InitialProjectRenderingState,\n ProjectMetaKey,\n InitialBlockMeta,\n blockArgsAuthorKey,\n ProjectLastModifiedTimestamp,\n ProjectCreatedTimestamp,\n ProjectStructureAuthorKey,\n getServiceTemplateField,\n FieldsToDuplicate,\n} from \"../model/project_model\";\nimport { BlockPackTemplateField, createBlockPack } from \"./block-pack/block_pack\";\nimport type { BlockGraph, ProductionGraphBlockInfo } from \"../model/project_model_util\";\nimport { allBlocks, graphDiff, productionGraph, stagingGraph } from \"../model/project_model_util\";\nimport type { BlockPackSpecPrepared } from \"../model\";\nimport type {\n AuthorMarker,\n BlockPackSpec,\n BlockSettings,\n ProjectMeta,\n} from \"@milaboratories/pl-model-middle-layer\";\nimport { InitialBlockSettings } from \"@milaboratories/pl-model-middle-layer\";\nimport Denque from \"denque\";\nimport { exportContext, getPreparedExportTemplateEnvelope } from \"./context_export\";\nimport { loadTemplate } from \"./template/template_loading\";\nimport {\n cachedDeserialize,\n notEmpty,\n canonicalJsonBytes,\n cachedDecode,\n type MiLogger,\n ConsoleLoggerAdapter,\n} from \"@milaboratories/ts-helpers\";\nimport type { ProjectHelper } from \"../model/project_helper\";\nimport {\n extractConfig,\n UiError,\n BLOCK_STORAGE_FACADE_VERSION,\n type BlockConfig,\n} from \"@platforma-sdk/model\";\nimport { getDebugFlags } from \"../debug\";\nimport type { BlockPackInfo } from \"../model/block_pack\";\n\ntype FieldStatus = \"NotReady\" | \"Ready\" | \"Error\";\n\ninterface BlockFieldState {\n modCount: number;\n ref?: AnyRef;\n status?: FieldStatus;\n value?: Uint8Array;\n}\n\ntype BlockFieldStates = Partial<Record<ProjectField[\"fieldName\"], BlockFieldState>>;\ntype BlockFieldStateValue = Omit<BlockFieldState, \"modCount\">;\n\ninterface BlockInfoState {\n readonly id: string;\n readonly fields: BlockFieldStates;\n blockConfig?: BlockConfig;\n blockPack?: BlockPackSpec;\n}\n\nfunction cached<ModId, T>(modIdCb: () => ModId, valueCb: () => T): () => T {\n let initialized = false;\n let lastModId: ModId | undefined = undefined;\n let value: T | undefined = undefined;\n return () => {\n if (!initialized) {\n initialized = true;\n lastModId = modIdCb();\n value = valueCb();\n return value;\n }\n const currentModId = modIdCb();\n if (lastModId !== currentModId) {\n lastModId = currentModId;\n value = valueCb();\n }\n return valueCb();\n };\n}\n\nclass BlockInfo {\n constructor(\n public readonly id: string,\n public readonly fields: BlockFieldStates,\n public readonly config: BlockConfig,\n public readonly source: BlockPackSpec,\n private readonly logger: MiLogger = new ConsoleLoggerAdapter(),\n ) {}\n\n public check() {\n // state assertions\n\n if ((this.fields.prodOutput === undefined) !== (this.fields.prodCtx === undefined))\n throw new Error(\"inconsistent prod fields\");\n\n if ((this.fields.stagingOutput === undefined) !== (this.fields.stagingCtx === undefined))\n throw new Error(\"inconsistent stage fields\");\n\n if (\n (this.fields.prodOutputPrevious === undefined) !==\n (this.fields.prodCtxPrevious === undefined)\n )\n throw new Error(\"inconsistent prod cache fields\");\n\n if (\n (this.fields.stagingOutputPrevious === undefined) !==\n (this.fields.stagingCtxPrevious === undefined)\n )\n throw new Error(\"inconsistent stage cache fields\");\n\n if (this.fields.blockPack === undefined) throw new Error(\"no block pack field\");\n\n if (this.fields.blockStorage === undefined) throw new Error(\"no block storage field\");\n }\n\n private readonly currentArgsC = cached(\n () => this.fields.currentArgs?.modCount,\n () => {\n const bin = this.fields.currentArgs?.value;\n if (bin === undefined) return undefined;\n return cachedDeserialize(bin);\n },\n );\n\n private readonly blockStorageC = cached(\n () => this.fields.blockStorage!.modCount,\n () => {\n const bin = this.fields.blockStorage?.value;\n if (bin === undefined) return undefined;\n return cachedDeserialize<Record<string, unknown>>(bin);\n },\n );\n\n private readonly blockStorageJ = cached(\n () => this.fields.blockStorage!.modCount,\n () => {\n const bin = this.fields.blockStorage?.value;\n if (bin === undefined) return undefined;\n return cachedDecode(bin);\n },\n );\n\n private readonly prodArgsC = cached(\n () => this.fields.prodArgs?.modCount,\n () => {\n const bin = this.fields.prodArgs?.value;\n if (bin === undefined) return undefined;\n return cachedDeserialize(bin);\n },\n );\n\n private readonly currentPrerunArgsC = cached(\n () => this.fields.currentPrerunArgs?.modCount,\n () => {\n const bin = this.fields.currentPrerunArgs?.value;\n if (bin === undefined) return undefined;\n return cachedDeserialize(bin);\n },\n );\n\n get currentArgs(): unknown {\n return this.currentArgsC();\n }\n\n get blockStorage(): unknown {\n try {\n return this.blockStorageC();\n } catch (e) {\n this.logger.error(new Error(`Error getting blockStorage for ${this.id}`, { cause: e }));\n return undefined;\n }\n }\n\n get blockStorageJson() {\n return this.blockStorageJ();\n }\n\n get currentPrerunArgs(): unknown {\n return this.currentPrerunArgsC();\n }\n\n get stagingRendered(): boolean {\n return this.fields.stagingCtx !== undefined;\n }\n\n get productionRendered(): boolean {\n return this.fields.prodCtx !== undefined;\n }\n\n get productionHasErrors(): boolean {\n return this.fields.prodUiCtx?.status === \"Error\";\n }\n\n private readonly productionStaleC: () => boolean = cached(\n () => `${this.fields.currentArgs!.modCount}_${this.fields.prodArgs?.modCount}`,\n () =>\n this.fields.prodArgs === undefined ||\n Buffer.compare(this.fields.currentArgs!.value!, this.fields.prodArgs.value!) !== 0,\n );\n\n get requireProductionRendering(): boolean {\n return !this.productionRendered || this.productionStaleC() || this.productionHasErrors;\n }\n\n /** Returns true if staging should be re-rendered (stagingCtx is not set) */\n get requireStagingRendering(): boolean {\n // No staging needed if currentPrerunArgs is undefined (args derivation failed)\n if (this.fields.currentPrerunArgs === undefined) return false;\n return !this.stagingRendered;\n }\n\n get prodArgs(): unknown {\n return this.prodArgsC();\n }\n\n public getTemplate(tx: PlTransaction): AnyRef {\n return tx.getFutureFieldValue(\n Pl.unwrapHolder(tx, this.fields.blockPack!.ref!),\n BlockPackTemplateField,\n \"Input\",\n );\n }\n}\n\n/**\n * Specification for creating a new block.\n * Discriminated union based on `storageMode`.\n */\n/** Specification for creating a new block. Discriminated union based on `storageMode`. */\nexport type NewBlockSpec =\n | { storageMode: \"fromModel\"; blockPack: BlockPackSpecPrepared }\n | { storageMode: \"legacy\"; blockPack: BlockPackSpecPrepared; legacyState: string };\n\nconst NoNewBlocks = (blockId: string) => {\n throw new Error(`No new block info for ${blockId}`);\n};\n\n/**\n * Request to set block state using unified state format.\n * For v3 blocks: state is the block's state\n * For v1/v2 blocks: state should be { args, uiState } format\n */\nexport type SetStatesRequest =\n | {\n blockId: string;\n /** The unified state to set */\n state: unknown;\n modelAPIVersion: 1;\n }\n | {\n blockId: string;\n /** Storage operation payload - middle layer is agnostic to specific operations */\n payload: { operation: string; value: unknown };\n modelAPIVersion: 2;\n };\n\nexport type ClearState = {\n state: unknown;\n};\n\nexport class ProjectMutator {\n private globalModCount = 0;\n private fieldsChanged: boolean = false;\n\n //\n // Change trackers\n //\n\n private lastModifiedChanged = false;\n private structureChanged = false;\n private metaChanged = false;\n private renderingStateChanged = false;\n\n /** Set blocks will be assigned current mutator author marker on save */\n private readonly blocksWithChangedInputs = new Set<string>();\n\n constructor(\n public readonly rid: ResourceId,\n private readonly tx: PlTransaction,\n private readonly author: AuthorMarker | undefined,\n private readonly schema: string,\n private lastModified: number,\n private meta: ProjectMeta,\n private struct: ProjectStructure,\n private readonly renderingState: Omit<ProjectRenderingState, \"blocksInLimbo\">,\n private readonly blocksInLimbo: Set<string>,\n private readonly blockInfos: Map<string, BlockInfo>,\n private readonly ctxExportTplHolder: AnyResourceRef,\n private readonly projectHelper: ProjectHelper,\n ) {}\n\n private fixProblemsAndMigrate() {\n // Fixing problems introduced by old code\n this.blockInfos.forEach((blockInfo) => {\n if (\n blockInfo.fields.prodArgs === undefined ||\n blockInfo.fields.prodOutput === undefined ||\n blockInfo.fields.prodCtx === undefined\n )\n this.deleteBlockFields(blockInfo.id, \"prodArgs\", \"prodOutput\", \"prodCtx\");\n });\n\n // Migration for addition of block settings field\n let initialBlockSettings: Omit<BlockFieldState, \"modCount\"> | undefined;\n this.blockInfos.forEach((blockInfo) => {\n if (blockInfo.fields.blockSettings === undefined) {\n if (initialBlockSettings === undefined)\n initialBlockSettings = this.createJsonFieldValue(InitialBlockSettings);\n this.setBlockFieldObj(blockInfo.id, \"blockSettings\", initialBlockSettings);\n }\n });\n }\n\n get wasModified(): boolean {\n return (\n this.lastModifiedChanged ||\n this.structureChanged ||\n this.fieldsChanged ||\n this.metaChanged ||\n this.renderingStateChanged\n );\n }\n\n get structure(): ProjectStructure {\n return JSON.parse(JSON.stringify(this.struct)) as ProjectStructure;\n }\n\n //\n // Graph calculation\n //\n\n private stagingGraph: BlockGraph | undefined = undefined;\n private pendingProductionGraph: BlockGraph | undefined = undefined;\n private actualProductionGraph: BlockGraph | undefined = undefined;\n\n private getStagingGraph(): BlockGraph {\n if (this.stagingGraph === undefined) this.stagingGraph = stagingGraph(this.struct);\n return this.stagingGraph;\n }\n\n private getProductionGraphBlockInfo(\n blockId: string,\n prod: boolean,\n ): ProductionGraphBlockInfo | undefined {\n const bInfo = this.getBlockInfo(blockId);\n\n let argsField: BlockFieldState | undefined;\n let args: unknown;\n\n if (prod) {\n if (bInfo.fields.prodArgs === undefined) return undefined;\n argsField = bInfo.fields.prodArgs;\n args = bInfo.prodArgs;\n } else {\n argsField = bInfo.fields.currentArgs;\n args = bInfo.currentArgs;\n }\n\n // Can't compute enrichment targets without args\n if (argsField === undefined) {\n return { args, enrichmentTargets: undefined };\n }\n\n const blockPackField = notEmpty(bInfo.fields.blockPack);\n\n if (isResourceId(argsField.ref!) && isResourceId(blockPackField.ref!))\n return {\n args,\n enrichmentTargets: this.projectHelper.getEnrichmentTargets(\n () => bInfo.config,\n () => args,\n { argsRid: argsField.ref, blockPackRid: blockPackField.ref },\n ),\n };\n else\n return {\n args,\n enrichmentTargets: this.projectHelper.getEnrichmentTargets(\n () => bInfo.config,\n () => args,\n ),\n };\n }\n\n private getPendingProductionGraph(): BlockGraph {\n if (this.pendingProductionGraph === undefined)\n this.pendingProductionGraph = productionGraph(this.struct, (blockId) =>\n this.getProductionGraphBlockInfo(blockId, false),\n );\n return this.pendingProductionGraph;\n }\n\n private getActualProductionGraph(): BlockGraph {\n if (this.actualProductionGraph === undefined)\n this.actualProductionGraph = productionGraph(this.struct, (blockId) =>\n this.getProductionGraphBlockInfo(blockId, true),\n );\n return this.actualProductionGraph;\n }\n\n //\n // Generic helpers to interact with project state\n //\n\n private getBlockInfo(blockId: string): BlockInfo {\n const info = this.blockInfos.get(blockId);\n if (info === undefined) throw new Error(`No such block: ${blockId}`);\n return info;\n }\n\n private createJsonFieldValueByContent(content: string): BlockFieldStateValue {\n if (content === undefined) throw new Error(\"content is undefined\");\n const value = Buffer.from(content);\n const ref = this.tx.createValue(Pl.JsonObject, value);\n return { ref, value, status: \"Ready\" };\n }\n\n private createJsonFieldValue(obj: unknown): BlockFieldStateValue {\n return this.createJsonFieldValueByContent(JSON.stringify(obj));\n }\n\n private getBlock(blockId: string): Block {\n for (const block of allBlocks(this.struct)) if (block.id === blockId) return block;\n throw new Error(\"block not found\");\n }\n\n private setBlockFieldObj(\n blockId: string,\n fieldName: keyof BlockFieldStates,\n state: BlockFieldStateValue,\n ) {\n const fid = field(this.rid, projectFieldName(blockId, fieldName));\n\n if (state.ref === undefined) throw new Error(\"Can't set value with empty ref\");\n\n if (this.getBlockInfo(blockId).fields[fieldName] === undefined)\n this.tx.createField(fid, \"Dynamic\", state.ref);\n else this.tx.setField(fid, state.ref);\n\n this.getBlockInfo(blockId).fields[fieldName] = {\n modCount: this.globalModCount++,\n ...state,\n };\n\n this.fieldsChanged = true;\n }\n\n private setBlockField(\n blockId: string,\n fieldName: keyof BlockFieldStates,\n ref: AnyRef,\n status: FieldStatus,\n value?: Uint8Array,\n ) {\n this.setBlockFieldObj(blockId, fieldName, { ref, status, value });\n }\n\n private deleteBlockFields(blockId: string, ...fieldNames: (keyof BlockFieldStates)[]): boolean {\n let deleted = false;\n const info = this.getBlockInfo(blockId);\n for (const fieldName of fieldNames) {\n const fields = info.fields;\n if (!(fieldName in fields)) continue;\n this.tx.removeField(field(this.rid, projectFieldName(blockId, fieldName)));\n delete fields[fieldName];\n this.fieldsChanged = true;\n deleted = true;\n }\n return deleted;\n }\n\n private updateLastModified() {\n this.lastModified = Date.now();\n this.lastModifiedChanged = true;\n }\n\n //\n // Main project actions\n //\n\n private resetStagingRefreshTimestamp() {\n this.renderingState.stagingRefreshTimestamp = Date.now();\n this.renderingStateChanged = true;\n }\n\n private resetStaging(blockId: string): void {\n const fields = this.getBlockInfo(blockId).fields;\n if (\n fields.stagingOutput?.status === \"Ready\" &&\n fields.stagingCtx?.status === \"Ready\" &&\n fields.stagingUiCtx?.status === \"Ready\"\n ) {\n this.setBlockFieldObj(blockId, \"stagingOutputPrevious\", fields.stagingOutput);\n this.setBlockFieldObj(blockId, \"stagingCtxPrevious\", fields.stagingCtx);\n this.setBlockFieldObj(blockId, \"stagingUiCtxPrevious\", fields.stagingUiCtx);\n }\n if (this.deleteBlockFields(blockId, \"stagingOutput\", \"stagingCtx\", \"stagingUiCtx\"))\n this.resetStagingRefreshTimestamp();\n }\n\n private resetProduction(blockId: string): void {\n const fields = this.getBlockInfo(blockId).fields;\n if (\n fields.prodOutput?.status === \"Ready\" &&\n fields.prodCtx?.status === \"Ready\" &&\n fields.prodUiCtx?.status === \"Ready\"\n ) {\n this.setBlockFieldObj(blockId, \"prodOutputPrevious\", fields.prodOutput);\n this.setBlockFieldObj(blockId, \"prodCtxPrevious\", fields.prodCtx);\n this.setBlockFieldObj(blockId, \"prodUiCtxPrevious\", fields.prodUiCtx);\n }\n this.deleteBlockFields(blockId, \"prodOutput\", \"prodCtx\", \"prodUiCtx\", \"prodArgs\");\n }\n\n /** Running blocks are reset, already computed moved to limbo. Returns if\n * either of the actions were actually performed.\n * This method ensures the block is left in a consistent state that passes check() constraints. */\n private resetOrLimboProduction(blockId: string): boolean {\n const fields = this.getBlockInfo(blockId).fields;\n\n // Check if we can safely move to limbo (both core production fields are ready)\n if (fields.prodOutput?.status === \"Ready\" && fields.prodCtx?.status === \"Ready\") {\n if (this.blocksInLimbo.has(blockId))\n // we are already in limbo\n return false;\n\n // limbo - keep the ready production results but clean up cache\n this.blocksInLimbo.add(blockId);\n this.renderingStateChanged = true;\n\n // doing some gc - clean up previous cache fields\n this.deleteBlockFields(blockId, \"prodOutputPrevious\", \"prodCtxPrevious\", \"prodUiCtxPrevious\");\n\n return true;\n } else {\n // reset - clean up any partial/inconsistent production stat\n return this.deleteBlockFields(\n blockId,\n \"prodOutput\",\n \"prodCtx\",\n \"prodUiCtx\",\n \"prodArgs\",\n \"prodOutputPrevious\",\n \"prodCtxPrevious\",\n \"prodUiCtxPrevious\",\n );\n }\n }\n\n /**\n * Gets current block state and merges with partial updates.\n * Used by legacy v1/v2 methods like setBlockArgs and setUiState.\n *\n * @param blockId The block to get state for\n * @param partialUpdate Partial state to merge (e.g. { args } or { uiState })\n * @returns Merged state in unified format { args, uiState }\n */\n public mergeBlockState(\n blockId: string,\n partialUpdate: { args?: unknown; uiState?: unknown },\n ): { args?: unknown; uiState?: unknown } {\n const info = this.getBlockInfo(blockId);\n const currentState = info.blockStorage as { args?: unknown; uiState?: unknown } | undefined;\n if (currentState === undefined) {\n throw new Error(`Cannot merge block state for ${blockId}: blockStorage is unavailable`);\n }\n return { ...currentState, ...partialUpdate };\n }\n\n /**\n * Sets raw block storage content directly (for testing purposes).\n * This bypasses all normalization and VM transformations.\n *\n * @param blockId The block to set storage for\n * @param rawStorageJson Raw storage as JSON string\n */\n public setBlockStorageRaw(blockId: string, rawStorageJson: string): void {\n this.setBlockFieldObj(\n blockId,\n \"blockStorage\",\n this.createJsonFieldValueByContent(rawStorageJson),\n );\n this.blocksWithChangedInputs.add(blockId);\n this.updateLastModified();\n }\n\n /**\n * Resets a v2+ block to its initial storage state.\n * Gets initial storage from VM and derives args from it.\n *\n * For v1 blocks, use setStates() instead.\n *\n * @param blockId The block to reset\n */\n public resetToInitialStorage(blockId: string): void {\n const info = this.getBlockInfo(blockId);\n const blockConfig = info.config;\n\n if (blockConfig.modelAPIVersion !== BLOCK_STORAGE_FACADE_VERSION) {\n throw new Error(\"resetToInitialStorage is only supported for model API version 2\");\n }\n\n // Get initial storage from VM\n const initialStorageJson = this.projectHelper.getInitialStorageInVM(blockConfig);\n this.setBlockStorageRaw(blockId, initialStorageJson);\n\n // Derive args from storage - set or clear currentArgs based on derivation result\n const deriveArgsResult = this.projectHelper.deriveArgsFromStorage(\n blockConfig,\n initialStorageJson,\n );\n if (!deriveArgsResult.error) {\n this.setBlockFieldObj(\n blockId,\n \"currentArgs\",\n this.createJsonFieldValue(deriveArgsResult.value),\n );\n // Derive prerunArgs from storage\n const prerunArgs = this.projectHelper.derivePrerunArgsFromStorage(\n blockConfig,\n initialStorageJson,\n );\n if (prerunArgs !== undefined) {\n this.setBlockFieldObj(blockId, \"currentPrerunArgs\", this.createJsonFieldValue(prerunArgs));\n } else {\n this.deleteBlockFields(blockId, \"currentPrerunArgs\");\n }\n } else {\n if (info.fields.currentPrerunArgs !== undefined) {\n this.projectHelper.logger.warn(\n `[staging] ${blockId}: currentPrerunArgs cleared (args derivation failed)`,\n );\n }\n this.deleteBlockFields(blockId, \"currentArgs\");\n this.deleteBlockFields(blockId, \"currentPrerunArgs\");\n }\n }\n\n /** Optimally sets inputs for multiple blocks in one go */\n public setStates(requests: SetStatesRequest[]) {\n const changedArgs: string[] = [];\n let somethingChanged = false;\n for (const req of requests) {\n const info = this.getBlockInfo(req.blockId);\n let blockChanged = false;\n\n const blockConfig = info.config;\n // modelAPIVersion === 2 means BlockModelV3 with .args() lambda for deriving args\n\n if (req.modelAPIVersion !== blockConfig.modelAPIVersion) {\n throw new Error(\n `Model API version mismatch for block ${req.blockId}: ${req.modelAPIVersion} !== ${blockConfig.modelAPIVersion}`,\n );\n }\n\n // Derive args from storage using the block's config.args() callback\n let args: unknown;\n let prerunArgs: unknown;\n\n if (req.modelAPIVersion === BLOCK_STORAGE_FACADE_VERSION) {\n const currentStorageJson = info.blockStorageJson;\n if (currentStorageJson === undefined) {\n throw new Error(`Block ${req.blockId} has no blockStorage - this should not happen`);\n }\n\n // Apply the state update to storage\n const updatedStorageJson = this.projectHelper.applyStorageUpdateInVM(\n blockConfig,\n currentStorageJson,\n req.payload,\n );\n\n this.setBlockFieldObj(\n req.blockId,\n \"blockStorage\",\n this.createJsonFieldValueByContent(updatedStorageJson),\n );\n\n // Derive args directly from storage (VM extracts data internally)\n const derivedArgsResult = this.projectHelper.deriveArgsFromStorage(\n blockConfig,\n updatedStorageJson,\n );\n if (derivedArgsResult.error) {\n args = undefined;\n prerunArgs = undefined;\n } else {\n args = derivedArgsResult.value;\n // Derive prerunArgs from storage, or fall back to args\n prerunArgs = this.projectHelper.derivePrerunArgsFromStorage(\n blockConfig,\n updatedStorageJson,\n );\n }\n } else {\n this.setBlockFieldObj(req.blockId, \"blockStorage\", this.createJsonFieldValue(req.state));\n if (req.state !== null && typeof req.state === \"object\" && \"args\" in req.state) {\n args = (req.state as { args: unknown }).args;\n } else {\n args = req.state;\n }\n // For the legacy blocks, prerunArgs = args (same as production args)\n prerunArgs = args;\n }\n\n // Set or clear currentArgs based on derivation result\n if (args !== undefined) {\n const currentArgsData = canonicalJsonBytes(args);\n const argsPartRef = this.tx.createValue(Pl.JsonObject, currentArgsData);\n this.setBlockField(req.blockId, \"currentArgs\", argsPartRef, \"Ready\", currentArgsData);\n } else {\n this.deleteBlockFields(req.blockId, \"currentArgs\");\n }\n\n // Set currentPrerunArgs field and check if it actually changed\n let prerunArgsChanged = false;\n if (prerunArgs !== undefined) {\n const prerunArgsData = canonicalJsonBytes(prerunArgs);\n const oldPrerunArgsData = info.fields.currentPrerunArgs?.value;\n // Check if prerunArgs actually changed\n if (\n oldPrerunArgsData === undefined ||\n Buffer.compare(oldPrerunArgsData, prerunArgsData) !== 0\n ) {\n prerunArgsChanged = true;\n }\n const prerunArgsRef = this.tx.createValue(Pl.JsonObject, prerunArgsData);\n this.setBlockField(\n req.blockId,\n \"currentPrerunArgs\",\n prerunArgsRef,\n \"Ready\",\n prerunArgsData,\n );\n } else {\n this.deleteBlockFields(req.blockId, \"currentPrerunArgs\");\n }\n\n blockChanged = true;\n // Only add to changedArgs if prerunArgs changed - this controls staging reset\n if (prerunArgsChanged) {\n changedArgs.push(req.blockId);\n }\n\n if (blockChanged) {\n // will be assigned our author marker\n this.blocksWithChangedInputs.add(req.blockId);\n somethingChanged = true;\n }\n }\n\n // resetting staging outputs for all downstream blocks\n this.getStagingGraph().traverse(\"downstream\", changedArgs, ({ id }) => this.resetStaging(id));\n\n if (somethingChanged) this.updateLastModified();\n }\n\n public setBlockSettings(blockId: string, newValue: BlockSettings): void {\n this.setBlockFieldObj(blockId, \"blockSettings\", this.createJsonFieldValue(newValue));\n this.updateLastModified();\n }\n\n private createProdCtx(upstream: Set<string>): AnyRef {\n const upstreamContexts: AnyRef[] = [];\n upstream.forEach((id) => {\n const info = this.getBlockInfo(id);\n if (info.fields[\"prodCtx\"]?.ref === undefined)\n throw new Error(\"One of the upstreams staging is not rendered.\");\n upstreamContexts.push(Pl.unwrapHolder(this.tx, info.fields[\"prodCtx\"].ref));\n });\n return createBContextFromUpstreams(this.tx, upstreamContexts);\n }\n\n private createStagingCtx(upstream: Set<string>): AnyRef {\n const upstreamContexts: AnyRef[] = [];\n upstream.forEach((id) => {\n const info = this.getBlockInfo(id);\n if (info.fields[\"stagingCtx\"]?.ref === undefined)\n throw new Error(\"One of the upstreams staging is not rendered.\");\n upstreamContexts.push(Pl.unwrapHolder(this.tx, info.fields[\"stagingCtx\"].ref));\n if (info.fields[\"prodCtx\"]?.ref !== undefined)\n upstreamContexts.push(Pl.unwrapHolder(this.tx, info.fields[\"prodCtx\"].ref));\n });\n return createBContextFromUpstreams(this.tx, upstreamContexts);\n }\n\n private exportCtx(ctx: AnyRef): AnyRef {\n return exportContext(this.tx, Pl.unwrapHolder(this.tx, this.ctxExportTplHolder), ctx);\n }\n\n /**\n * Renders staging for a block using currentPrerunArgs.\n * If currentPrerunArgs is not set (prerunArgs returned undefined), skips staging for this block.\n */\n private renderStagingFor(blockId: string) {\n this.resetStaging(blockId);\n\n const info = this.getBlockInfo(blockId);\n\n // If currentPrerunArgs is not set (prerunArgs returned undefined), skip staging for this block\n const prerunArgsRef = info.fields.currentPrerunArgs?.ref;\n if (prerunArgsRef === undefined) {\n return;\n }\n\n const ctx = this.createStagingCtx(this.getStagingGraph().nodes.get(blockId)!.upstream);\n\n if (this.getBlock(blockId).renderingMode !== \"Heavy\") throw new Error(\"not supported yet\");\n\n const tpl = info.getTemplate(this.tx);\n\n // Use currentPrerunArgs for staging rendering\n const results = createRenderHeavyBlock(this.tx, tpl, {\n args: prerunArgsRef,\n blockId: this.tx.createValue(Pl.JsonString, JSON.stringify(blockId)),\n isProduction: this.tx.createValue(Pl.JsonBool, JSON.stringify(false)),\n context: ctx,\n });\n\n // Here we set the staging ctx to the input context of the staging workflow, not the output because exports\n // of one staging context should stay within the same block, and not travel downstream.\n // We may change this decision in the future if wanted to support traveling staging exports downstream.\n this.setBlockField(blockId, \"stagingCtx\", Pl.wrapInEphHolder(this.tx, ctx), \"NotReady\");\n\n // Yet the staging UI Ctx is the output context of the staging workflow, because it is used to form the result pool\n // thus creating a certain discrepancy between staging workflow context behavior and desktop's result pool.\n this.setBlockField(blockId, \"stagingUiCtx\", this.exportCtx(results.context), \"NotReady\");\n this.setBlockField(blockId, \"stagingOutput\", results.result, \"NotReady\");\n }\n\n private renderProductionFor(blockId: string) {\n this.resetProduction(blockId);\n\n const info = this.getBlockInfo(blockId);\n\n // Can't render production if currentArgs is not set\n if (info.fields.currentArgs === undefined) {\n throw new Error(`Can't render production for block ${blockId}: currentArgs not set`);\n }\n\n const ctx = this.createProdCtx(this.getPendingProductionGraph().nodes.get(blockId)!.upstream);\n\n if (this.getBlock(blockId).renderingMode === \"Light\")\n throw new Error(\"Can't render production for light block.\");\n\n const tpl = info.getTemplate(this.tx);\n\n const results = createRenderHeavyBlock(this.tx, tpl, {\n args: info.fields.currentArgs.ref!,\n blockId: this.tx.createValue(Pl.JsonString, JSON.stringify(blockId)),\n isProduction: this.tx.createValue(Pl.JsonBool, JSON.stringify(true)),\n context: ctx,\n });\n this.setBlockField(\n blockId,\n \"prodCtx\",\n Pl.wrapInEphHolder(this.tx, results.context),\n \"NotReady\",\n );\n this.setBlockField(blockId, \"prodUiCtx\", this.exportCtx(results.context), \"NotReady\");\n this.setBlockField(blockId, \"prodOutput\", results.result, \"NotReady\");\n\n // saving inputs for which we rendered the production\n this.setBlockFieldObj(blockId, \"prodArgs\", info.fields.currentArgs);\n\n // removing block from limbo as we juts rendered fresh production for it\n if (this.blocksInLimbo.delete(blockId)) this.renderingStateChanged = true;\n }\n\n //\n // Structure changes\n //\n\n private initializeNewBlock(blockId: string, spec: NewBlockSpec): void {\n const info = new BlockInfo(\n blockId,\n {},\n extractConfig(spec.blockPack.config),\n spec.blockPack.source,\n this.projectHelper.logger,\n );\n this.blockInfos.set(blockId, info);\n\n // block pack\n const bp = createBlockPack(this.tx, spec.blockPack);\n this.setBlockField(blockId, \"blockPack\", Pl.wrapInHolder(this.tx, bp), \"NotReady\");\n\n // settings\n this.setBlockFieldObj(\n blockId,\n \"blockSettings\",\n this.createJsonFieldValue(InitialBlockSettings),\n );\n\n const blockConfig = info.config;\n\n let args: unknown;\n let prerunArgs: unknown;\n let storageToWrite: string;\n\n if (spec.storageMode === \"fromModel\") {\n // Model API v2+: get initial storage and derive args from it\n storageToWrite = this.projectHelper.getInitialStorageInVM(blockConfig);\n\n // Derive args directly from storage (VM extracts data internally)\n const deriveArgsResult = this.projectHelper.deriveArgsFromStorage(\n blockConfig,\n storageToWrite,\n );\n if (deriveArgsResult.error) {\n args = undefined;\n prerunArgs = undefined;\n } else {\n args = deriveArgsResult.value;\n prerunArgs = this.projectHelper.derivePrerunArgsFromStorage(blockConfig, storageToWrite);\n }\n } else if (spec.storageMode === \"legacy\") {\n // Model API v1: use legacyState from spec\n const parsedState = JSON.parse(spec.legacyState);\n args = parsedState.args;\n if (args === undefined) {\n throw new Error(\"args is undefined in legacyState\");\n }\n prerunArgs = args;\n storageToWrite = spec.legacyState;\n } else {\n throw new Error(`Unknown storageMode: ${(spec as NewBlockSpec).storageMode}`);\n }\n\n // currentArgs\n if (args !== undefined) {\n this.setBlockFieldObj(blockId, \"currentArgs\", this.createJsonFieldValue(args));\n }\n\n // currentPrerunArgs\n if (prerunArgs !== undefined) {\n this.setBlockFieldObj(blockId, \"currentPrerunArgs\", this.createJsonFieldValue(prerunArgs));\n }\n\n // blockStorage\n this.setBlockFieldObj(\n blockId,\n \"blockStorage\",\n this.createJsonFieldValueByContent(storageToWrite),\n );\n\n // checking structure\n info.check();\n }\n\n private getFieldNamesToDuplicate(blockId: string): Set<ProjectField[\"fieldName\"]> {\n const fields = this.getBlockInfo(blockId).fields;\n\n const diff = <T>(setA: Set<T>, setB: Set<T>): Set<T> =>\n new Set([...setA].filter((x) => !setB.has(x)));\n\n // Check if we can safely move to limbo (both core production fields are ready)\n if (fields.prodOutput?.status === \"Ready\" && fields.prodCtx?.status === \"Ready\") {\n if (this.blocksInLimbo.has(blockId))\n // we are already in limbo\n return FieldsToDuplicate;\n\n return diff(\n FieldsToDuplicate,\n new Set([\"prodOutputPrevious\", \"prodCtxPrevious\", \"prodUiCtxPrevious\"]),\n );\n } else {\n return diff(\n FieldsToDuplicate,\n new Set([\n \"prodOutput\",\n \"prodCtx\",\n \"prodUiCtx\",\n \"prodArgs\",\n \"prodOutputPrevious\",\n \"prodCtxPrevious\",\n \"prodUiCtxPrevious\",\n ]),\n );\n }\n }\n\n private initializeBlockDuplicate(blockId: string, originalBlockInfo: BlockInfo) {\n const info = new BlockInfo(\n blockId,\n {},\n originalBlockInfo.config,\n originalBlockInfo.source,\n this.projectHelper.logger,\n );\n\n this.blockInfos.set(blockId, info);\n\n const fieldNamesToDuplicate = this.getFieldNamesToDuplicate(blockId);\n\n // Copy all fields from original block to new block by sharing references\n for (const [fieldName, fieldState] of Object.entries(originalBlockInfo.fields)) {\n if (\n fieldNamesToDuplicate.has(fieldName as ProjectField[\"fieldName\"]) &&\n fieldState &&\n fieldState.ref\n ) {\n this.setBlockFieldObj(blockId, fieldName as keyof BlockFieldStates, {\n ref: fieldState.ref,\n status: fieldState.status,\n value: fieldState.value,\n });\n }\n }\n\n this.resetOrLimboProduction(blockId);\n\n info.check();\n }\n\n /** Very generic method, better check for more specialized case-specific methods first. */\n public updateStructure(\n newStructure: ProjectStructure,\n newBlockInitializer: (blockId: string) => void = NoNewBlocks,\n ): void {\n const currentStagingGraph = this.getStagingGraph();\n const currentActualProductionGraph = this.getActualProductionGraph();\n\n const newStagingGraph = stagingGraph(newStructure);\n\n const stagingDiff = graphDiff(currentStagingGraph, newStagingGraph);\n\n // removing blocks\n for (const blockId of stagingDiff.onlyInA) {\n const { fields } = this.getBlockInfo(blockId);\n this.deleteBlockFields(blockId, ...(Object.keys(fields) as ProjectField[\"fieldName\"][]));\n this.blockInfos.delete(blockId);\n if (this.blocksInLimbo.delete(blockId)) this.renderingStateChanged = true;\n }\n\n // creating new blocks\n for (const blockId of stagingDiff.onlyInB) {\n newBlockInitializer(blockId);\n }\n\n // resetting stagings affected by topology change\n for (const blockId of stagingDiff.different) this.resetStaging(blockId);\n\n // new actual production graph without new blocks\n const newActualProductionGraph = productionGraph(newStructure, (blockId) =>\n this.getProductionGraphBlockInfo(blockId, true),\n );\n\n const prodDiff = graphDiff(currentActualProductionGraph, newActualProductionGraph);\n\n // applying changes due to topology change in production to affected nodes and\n // all their downstreams\n currentActualProductionGraph.traverse(\"downstream\", [...prodDiff.different], (node) => {\n this.resetOrLimboProduction(node.id);\n });\n\n if (\n stagingDiff.onlyInB.size > 0 ||\n stagingDiff.onlyInA.size > 0 ||\n stagingDiff.different.size > 0\n )\n this.resetStagingRefreshTimestamp();\n\n this.struct = newStructure;\n this.structureChanged = true;\n this.stagingGraph = undefined;\n this.pendingProductionGraph = undefined;\n this.actualProductionGraph = undefined;\n\n this.updateLastModified();\n }\n\n //\n // Structure change helpers\n //\n\n public addBlock(block: Block, spec: NewBlockSpec, before?: string): void {\n const newStruct = this.structure; // copy current structure\n if (before === undefined) {\n // adding as a very last block\n newStruct.groups[newStruct.groups.length - 1].blocks.push(block);\n } else {\n let done = false;\n for (const group of newStruct.groups) {\n const idx = group.blocks.findIndex((b) => b.id === before);\n if (idx < 0) continue;\n group.blocks.splice(idx, 0, block);\n done = true;\n break;\n }\n if (!done) throw new Error(`Can't find element with id: ${before}`);\n }\n this.updateStructure(newStruct, (blockId) => {\n if (blockId !== block.id) throw new Error(\"Unexpected\");\n this.initializeNewBlock(blockId, spec);\n });\n }\n\n /**\n * Duplicates an existing block by copying all its fields and structure.\n * This method creates a deep copy of the block at the mutator level.\n *\n * @param originalBlockId id of the block to duplicate\n * @param newBlockId id for the new duplicated block\n * @param after id of the block to insert new block after\n */\n public duplicateBlock(originalBlockId: string, newBlockId: string, after?: string): void {\n // Get the original block from structure\n const originalBlock = this.getBlock(originalBlockId);\n const originalBlockInfo = this.getBlockInfo(originalBlockId);\n\n // Create new block in structure\n const newBlock: Block = {\n id: newBlockId,\n label: originalBlock.label,\n renderingMode: originalBlock.renderingMode,\n };\n\n // Add the new block to structure\n const newStruct = this.structure; // copy current structure\n if (after === undefined) {\n // adding as a very last block\n newStruct.groups[newStruct.groups.length - 1].blocks.push(newBlock);\n } else {\n let done = false;\n for (const group of newStruct.groups) {\n const idx = group.blocks.findIndex((b) => b.id === after);\n if (idx < 0) continue;\n group.blocks.splice(idx + 1, 0, newBlock);\n done = true;\n break;\n }\n if (!done) throw new Error(`Can't find element with id: ${after}`);\n }\n\n this.updateStructure(newStruct, (blockId) => {\n if (blockId !== newBlockId) throw new Error(\"Unexpected\");\n this.initializeBlockDuplicate(blockId, originalBlockInfo);\n });\n }\n\n public deleteBlock(blockId: string): void {\n const newStruct = this.structure; // copy current structure\n let done = false;\n for (const group of newStruct.groups) {\n const idx = group.blocks.findIndex((b) => b.id === blockId);\n if (idx < 0) continue;\n group.blocks.splice(idx, 1);\n done = true;\n break;\n }\n if (!done) throw new Error(`Can't find element with id: ${blockId}`);\n this.updateStructure(newStruct);\n }\n\n //\n // Block-pack migration\n //\n\n public migrateBlockPack(\n blockId: string,\n spec: BlockPackSpecPrepared,\n newClearState?: ClearState,\n ): void {\n const info = this.getBlockInfo(blockId);\n const newConfig = extractConfig(spec.config);\n\n const persistBlockPack = () => {\n this.setBlockField(\n blockId,\n \"blockPack\",\n Pl.wrapInHolder(this.tx, createBlockPack(this.tx, spec)),\n \"NotReady\",\n );\n };\n\n const applyStorageAndDeriveArgs = (storageJson: string) => {\n persistBlockPack();\n this.setBlockStorageRaw(blockId, storageJson);\n const deriveArgsResult = this.projectHelper.deriveArgsFromStorage(newConfig, storageJson);\n if (!deriveArgsResult.error) {\n this.setBlockFieldObj(\n blockId,\n \"currentArgs\",\n this.createJsonFieldValue(deriveArgsResult.value),\n );\n const prerunArgs = this.projectHelper.derivePrerunArgsFromStorage(newConfig, storageJson);\n if (prerunArgs !== undefined) {\n this.setBlockFieldObj(\n blockId,\n \"currentPrerunArgs\",\n this.createJsonFieldValue(prerunArgs),\n );\n } else {\n this.deleteBlockFields(blockId, \"currentPrerunArgs\");\n }\n } else {\n this.deleteBlockFields(blockId, \"currentArgs\");\n this.deleteBlockFields(blockId, \"currentPrerunArgs\");\n }\n };\n\n if (newClearState !== undefined) {\n // State is being reset - no migration needed\n const supportsStorageFromVM = newConfig.modelAPIVersion === BLOCK_STORAGE_FACADE_VERSION;\n\n if (supportsStorageFromVM) {\n // V2+: Get initial storage directly from VM and derive args from it\n const initialStorageJson = this.projectHelper.getInitialStorageInVM(newConfig);\n applyStorageAndDeriveArgs(initialStorageJson);\n this.blocksWithChangedInputs.add(blockId);\n this.updateLastModified();\n } else {\n // V1: Use setStates with legacy state format\n persistBlockPack();\n this.setStates([{ modelAPIVersion: 1, blockId, state: newClearState.state }]);\n }\n } else {\n // State is being preserved - run migrations if needed via VM\n // Only Model API v2 blocks support migrations\n const supportsStateMigrations = newConfig.modelAPIVersion === BLOCK_STORAGE_FACADE_VERSION;\n\n if (supportsStateMigrations) {\n const currentStorageJson = info.blockStorageJson;\n\n // Attempt migration BEFORE persisting block pack — on failure,\n // block stays on old version (no inconsistent new-code/old-storage state)\n const migrationResult = this.projectHelper.migrateStorageInVM(\n newConfig,\n currentStorageJson,\n );\n\n if (migrationResult.error !== undefined) {\n throw new Error(\n `[migrateBlockPack] Block ${blockId} migration failed: ${migrationResult.error}`,\n );\n }\n\n this.projectHelper.logger.info(\n `[migrateBlockPack] Block ${blockId}: ${migrationResult.info}`,\n );\n applyStorageAndDeriveArgs(migrationResult.newStorageJson);\n } else {\n // Legacy blocks (modelAPIVersion 1): persist block pack, set prerunArgs = currentArgs\n persistBlockPack();\n if (info.fields.currentArgs !== undefined) {\n this.setBlockFieldObj(blockId, \"currentPrerunArgs\", info.fields.currentArgs);\n }\n }\n\n this.blocksWithChangedInputs.add(blockId);\n\n // resetting staging outputs for all downstream blocks\n this.getStagingGraph().traverse(\"downstream\", [blockId], ({ id }) => this.resetStaging(id));\n }\n\n // also reset or limbo all downstream productions\n if (info.productionRendered)\n this.getActualProductionGraph().traverse(\"downstream\", [blockId], ({ id }) =>\n this.resetOrLimboProduction(id),\n );\n\n this.updateLastModified();\n }\n\n //\n // Render\n //\n\n public renderProduction(blockIds: string[], addUpstreams: boolean = false): Set<string> {\n const blockIdsSet = new Set(blockIds);\n\n const prodGraph = this.getPendingProductionGraph();\n if (addUpstreams)\n // adding all upstreams automatically\n prodGraph.traverse(\"upstream\", blockIds, (node) => {\n blockIdsSet.add(node.id);\n });\n else\n // checking that targets contain all upstreams\n for (const blockId of blockIdsSet) {\n const node = prodGraph.nodes.get(blockId);\n if (node === undefined) throw new Error(`Can't find block with id: ${blockId}`);\n for (const upstream of node.upstream)\n if (!blockIdsSet.has(upstream))\n throw new Error(\"Can't render blocks not including all upstreams.\");\n }\n\n // traversing in topological order and rendering target blocks\n const rendered = new Set<string>();\n for (const block of allBlocks(this.structure)) {\n if (!blockIdsSet.has(block.id)) continue;\n\n let render =\n this.getBlockInfo(block.id).requireProductionRendering || this.blocksInLimbo.has(block.id);\n\n if (!render)\n for (const upstream of prodGraph.nodes.get(block.id)!.upstream)\n if (rendered.has(upstream)) {\n render = true;\n break;\n }\n\n if (render) {\n this.renderProductionFor(block.id);\n rendered.add(block.id);\n }\n }\n\n const renderedArray = [...rendered];\n\n // sending to limbo all downstream blocks\n prodGraph.traverse(\"downstream\", renderedArray, (node) => {\n if (rendered.has(node.id))\n // don't send to limbo blocks that were just rendered\n return;\n this.resetOrLimboProduction(node.id);\n });\n\n // resetting staging outputs for all downstream blocks\n this.getStagingGraph().traverse(\"downstream\", renderedArray, ({ id }) => {\n // don't reset staging of the first rendered block\n if (renderedArray[0] !== id) this.resetStaging(id);\n });\n\n if (rendered.size > 0) this.updateLastModified();\n\n return rendered;\n }\n\n /** Stops running blocks from the list and modify states of other blocks\n * accordingly */\n public stopProduction(...blockIds: string[]) {\n const activeProdGraph = this.getActualProductionGraph();\n\n // we will stop all blocks listed in request and all their downstreams\n const queue = new Denque(blockIds);\n const queued = new Set(blockIds);\n const stopped: string[] = [];\n\n while (!queue.isEmpty()) {\n const blockId = queue.shift()!;\n const fields = this.getBlockInfo(blockId).fields;\n\n if (fields.prodOutput?.status === \"Ready\" && fields.prodCtx?.status === \"Ready\")\n // skipping finished blocks\n continue;\n\n if (this.deleteBlockFields(blockId, \"prodOutput\", \"prodCtx\", \"prodUiCtx\", \"prodArgs\")) {\n // was actually stopped\n stopped.push(blockId);\n\n // will try to stop all its downstreams\n for (const downstream of activeProdGraph.traverseIdsExcludingRoots(\"downstream\", blockId)) {\n if (queued.has(downstream)) continue;\n queue.push(downstream);\n queued.add(downstream);\n }\n }\n }\n\n // blocks under stopped blocks, but having finished production results, goes to limbo\n for (const blockId of activeProdGraph.traverseIdsExcludingRoots(\"downstream\", ...stopped))\n this.resetOrLimboProduction(blockId);\n\n // reset staging outputs for all downstream blocks\n this.getStagingGraph().traverse(\"downstream\", stopped, ({ id }) => this.resetStaging(id));\n }\n\n private traverseWithStagingLag(cb: (blockId: string, lag: number) => void) {\n const lags = new Map<string, number>();\n const stagingGraph = this.getStagingGraph();\n stagingGraph.nodes.forEach((node) => {\n const info = this.getBlockInfo(node.id);\n // Use requireStagingRendering to check both: staging exists AND prerunArgs hasn't changed\n const requiresRendering = info.requireStagingRendering;\n let lag = requiresRendering ? 1 : 0;\n node.upstream.forEach((upstream) => {\n const upstreamLag = lags.get(upstream)!;\n if (upstreamLag === 0) return;\n lag = Math.max(upstreamLag + 1, lag);\n });\n if (!requiresRendering && info.stagingRendered) {\n // console.log(`[traverseWithStagingLag] SKIP staging for ${node.id} - prerunArgs unchanged`);\n }\n cb(node.id, lag);\n lags.set(node.id, lag);\n });\n }\n\n /** @param stagingRenderingRate rate in blocks per second */\n private refreshStagings(stagingRenderingRate?: number) {\n const elapsed = Date.now() - this.renderingState.stagingRefreshTimestamp;\n const lagThreshold =\n stagingRenderingRate === undefined\n ? undefined\n : 1 + Math.max(0, (elapsed * stagingRenderingRate) / 1000);\n let rendered = 0;\n this.traverseWithStagingLag((blockId, lag) => {\n if (lag === 0)\n // meaning staging already rendered\n return;\n if (lagThreshold === undefined || lag <= lagThreshold) {\n try {\n this.renderStagingFor(blockId);\n rendered++;\n } catch (e) {\n this.projectHelper.logger.error(\n new Error(`[refreshStagings] renderStagingFor failed for ${blockId}`, { cause: e }),\n );\n }\n }\n });\n if (rendered > 0) this.resetStagingRefreshTimestamp();\n }\n\n //\n // Meta\n //\n\n /** Updates project metadata */\n public setMeta(meta: ProjectMeta): void {\n this.meta = meta;\n this.metaChanged = true;\n this.updateLastModified();\n }\n\n //\n // Maintenance\n //\n\n /** @param stagingRenderingRate rate in blocks per second */\n public doRefresh(stagingRenderingRate?: number) {\n this.refreshStagings(stagingRenderingRate);\n this.blockInfos.forEach((blockInfo) => {\n if (\n blockInfo.fields.prodCtx?.status === \"Ready\" &&\n blockInfo.fields.prodOutput?.status === \"Ready\"\n )\n this.deleteBlockFields(\n blockInfo.id,\n \"prodOutputPrevious\",\n \"prodCtxPrevious\",\n \"prodUiCtxPrevious\",\n );\n if (\n blockInfo.fields.stagingCtx?.status === \"Ready\" &&\n blockInfo.fields.stagingOutput?.status === \"Ready\"\n )\n this.deleteBlockFields(\n blockInfo.id,\n \"stagingOutputPrevious\",\n \"stagingCtxPrevious\",\n \"stagingUiCtxPrevious\",\n );\n });\n }\n\n private assignAuthorMarkers() {\n const markerStr = this.author ? JSON.stringify(this.author) : undefined;\n\n for (const blockId of this.blocksWithChangedInputs)\n if (markerStr === undefined) this.tx.deleteKValue(this.rid, blockArgsAuthorKey(blockId));\n else this.tx.setKValue(this.rid, blockArgsAuthorKey(blockId), markerStr);\n\n if (this.metaChanged || this.structureChanged) {\n if (markerStr === undefined) this.tx.deleteKValue(this.rid, ProjectStructureAuthorKey);\n else this.tx.setKValue(this.rid, ProjectStructureAuthorKey, markerStr);\n }\n }\n\n public save() {\n if (!this.wasModified) return;\n\n if (this.lastModifiedChanged)\n this.tx.setKValue(this.rid, ProjectLastModifiedTimestamp, JSON.stringify(this.lastModified));\n\n if (this.structureChanged)\n this.tx.setKValue(this.rid, ProjectStructureKey, JSON.stringify(this.struct));\n\n if (this.renderingStateChanged)\n this.tx.setKValue(\n this.rid,\n BlockRenderingStateKey,\n JSON.stringify({\n ...this.renderingState,\n blocksInLimbo: [...this.blocksInLimbo],\n } as ProjectRenderingState),\n );\n\n if (this.metaChanged) this.tx.setKValue(this.rid, ProjectMetaKey, JSON.stringify(this.meta));\n\n this.assignAuthorMarkers();\n }\n\n public static async load(\n projectHelper: ProjectHelper,\n tx: PlTransaction,\n rid: ResourceId,\n author?: AuthorMarker,\n ): Promise<ProjectMutator> {\n //\n // Sending initial requests to read project state (start of round-trip #1)\n //\n\n const fullResourceStateP = tx.getResourceData(rid, true);\n const schemaP = tx.getKValueJson<string>(rid, SchemaVersionKey);\n const lastModifiedP = tx.getKValueJson<number>(rid, ProjectLastModifiedTimestamp);\n const metaP = tx.getKValueJson<ProjectMeta>(rid, ProjectMetaKey);\n const structureP = tx.getKValueJson<ProjectStructure>(rid, ProjectStructureKey);\n const renderingStateP = tx.getKValueJson<ProjectRenderingState>(rid, BlockRenderingStateKey);\n\n const fullResourceState = await fullResourceStateP;\n\n // loading field information\n const blockInfoStates = new Map<string, BlockInfoState>();\n for (const f of fullResourceState.fields) {\n const projectField = parseProjectField(f.name);\n\n // processing only fields with known structure\n if (projectField === undefined) continue;\n\n let info = blockInfoStates.get(projectField.blockId);\n if (info === undefined) {\n info = {\n id: projectField.blockId,\n fields: {},\n };\n blockInfoStates.set(projectField.blockId, info);\n }\n\n info.fields[projectField.fieldName] = isNullResourceId(f.value)\n ? { modCount: 0 }\n : { modCount: 0, ref: f.value };\n }\n\n //\n // Roundtrip #1 not yet finished, but as soon as field list is received,\n // we can start sending requests to read states of referenced resources\n // (start of round-trip #2)\n //\n\n const blockFieldRequests: [\n BlockInfoState,\n ProjectField[\"fieldName\"],\n BlockFieldState,\n Promise<BasicResourceData | ResourceData>,\n ][] = [];\n blockInfoStates.forEach((info) => {\n const fields = info.fields;\n for (const [fName, state] of Object.entries(fields)) {\n if (state.ref === undefined) continue;\n if (!isResource(state.ref) || isResourceRef(state.ref))\n throw new Error(\"unexpected behaviour\");\n const fieldName = fName as ProjectField[\"fieldName\"];\n blockFieldRequests.push([\n info,\n fieldName,\n state,\n tx.getResourceData(state.ref, fieldName == \"blockPack\"),\n ]);\n }\n });\n\n // loading jsons\n const [schema, lastModified, meta, structure, { stagingRefreshTimestamp, blocksInLimbo }] =\n await Promise.all([schemaP, lastModifiedP, metaP, structureP, renderingStateP]);\n\n // Checking schema version of the project\n if (schema !== SchemaVersionCurrent) {\n if (Number(schema) < Number(SchemaVersionCurrent))\n throw new UiError(\n `Can't perform this action on this project because it has older schema. Try (re)loading the project to update it.`,\n );\n else\n throw new UiError(\n `Can't perform this action on this project because it has newer schema. Upgrade your desktop app to the latest version.`,\n );\n }\n\n //\n // <- at this point we have all the responses from round-trip #1\n //\n\n //\n // Receiving responses from round-trip #2 and sending requests to read block pack descriptions\n // (start of round-trip #3)\n //\n\n const blockPackRequests: [BlockInfoState, Promise<BasicResourceData>][] = [];\n for (const [info, fieldName, state, response] of blockFieldRequests) {\n const result = await response;\n state.value = result.data;\n if (isNotNullResourceId(result.error)) state.status = \"Error\";\n else if (result.resourceReady || isNotNullResourceId(result.originalResourceId))\n state.status = \"Ready\";\n else state.status = \"NotReady\";\n\n // For block pack we need to traverse the ref field from the resource data\n if (fieldName === \"blockPack\") {\n const refField = (result as ResourceData).fields.find((f) => f.name === Pl.HolderRefField);\n if (refField === undefined) throw new Error(\"Block pack ref field is missing\");\n blockPackRequests.push([\n info,\n tx.getResourceData(ensureResourceIdNotNull(refField.value), false),\n ]);\n }\n }\n\n //\n // <- at this point we have all the responses from round-trip #2\n //\n\n for (const [info, response] of blockPackRequests) {\n const result = await response;\n const bpInfo = cachedDeserialize<BlockPackInfo>(notEmpty(result.data));\n info.blockConfig = extractConfig(bpInfo.config);\n info.blockPack = bpInfo.source;\n }\n\n //\n // <- at this point we have all the responses from round-trip #3\n //\n\n // loading ctx export template to check if we already have cached materialized template in our project\n const ctxExportTplEnvelope = await getPreparedExportTemplateEnvelope();\n\n // expected field name\n const ctxExportTplCacheFieldName = getServiceTemplateField(ctxExportTplEnvelope.hash);\n const ctxExportTplField = fullResourceState.fields.find(\n (f) => f.name === ctxExportTplCacheFieldName,\n );\n let ctxExportTplHolder: AnyResourceRef;\n if (ctxExportTplField !== undefined)\n ctxExportTplHolder = ensureResourceIdNotNull(ctxExportTplField.value);\n else {\n ctxExportTplHolder = Pl.wrapInHolder(tx, loadTemplate(tx, ctxExportTplEnvelope.spec));\n tx.createField(\n field(rid, getServiceTemplateField(ctxExportTplEnvelope.hash)),\n \"Dynamic\",\n ctxExportTplHolder,\n );\n }\n\n const renderingState = { stagingRefreshTimestamp };\n const blocksInLimboSet = new Set(blocksInLimbo);\n\n const blockInfos = new Map<string, BlockInfo>();\n blockInfoStates.forEach(({ id, fields, blockConfig, blockPack }) =>\n blockInfos.set(\n id,\n new BlockInfo(id, fields, notEmpty(blockConfig), notEmpty(blockPack), projectHelper.logger),\n ),\n );\n\n // check consistency of project state\n const blockInStruct = new Set<string>();\n for (const b of allBlocks(structure)) {\n if (!blockInfos.has(b.id))\n throw new Error(`Inconsistent project structure: no inputs for ${b.id}`);\n blockInStruct.add(b.id);\n }\n blockInfos.forEach((info) => {\n if (!blockInStruct.has(info.id))\n throw new Error(`Inconsistent project structure: no structure entry for ${info.id}`);\n // checking structure\n info.check();\n });\n\n const prj = new ProjectMutator(\n rid,\n tx,\n author,\n schema,\n lastModified,\n meta,\n structure,\n renderingState,\n blocksInLimboSet,\n blockInfos,\n ctxExportTplHolder,\n projectHelper,\n );\n\n prj.fixProblemsAndMigrate();\n\n return prj;\n }\n}\n\nexport interface ProjectState {\n schema: string;\n structure: ProjectStructure;\n renderingState: Omit<ProjectRenderingState, \"blocksInLimbo\">;\n blocksInLimbo: Set<string>;\n blockInfos: Map<string, BlockInfo>;\n}\n\nexport async function createProject(\n tx: PlTransaction,\n meta: ProjectMeta = InitialBlockMeta,\n): Promise<AnyResourceRef> {\n const prj = tx.createEphemeral(ProjectResourceType);\n tx.lock(prj);\n const ts = String(Date.now());\n tx.setKValue(prj, SchemaVersionKey, JSON.stringify(SchemaVersionCurrent));\n tx.setKValue(prj, ProjectCreatedTimestamp, ts);\n tx.setKValue(prj, ProjectLastModifiedTimestamp, ts);\n tx.setKValue(prj, ProjectMetaKey, JSON.stringify(meta));\n tx.setKValue(prj, ProjectStructureKey, JSON.stringify(InitialBlockStructure));\n tx.setKValue(prj, BlockRenderingStateKey, JSON.stringify(InitialProjectRenderingState));\n const ctxExportTplEnvelope = await getPreparedExportTemplateEnvelope();\n tx.createField(\n field(prj, getServiceTemplateField(ctxExportTplEnvelope.hash)),\n \"Dynamic\",\n Pl.wrapInHolder(tx, loadTemplate(tx, ctxExportTplEnvelope.spec)),\n );\n return prj;\n}\n\nexport async function withProject<T>(\n projectHelper: ProjectHelper,\n txOrPl: PlTransaction | PlClient,\n rid: ResourceId,\n cb: (p: ProjectMutator) => T | Promise<T>,\n ops?: Partial<TxOps>,\n): Promise<T> {\n return withProjectAuthored(projectHelper, txOrPl, rid, undefined, cb, ops);\n}\n\nexport async function withProjectAuthored<T>(\n projectHelper: ProjectHelper,\n txOrPl: PlTransaction | PlClient,\n rid: ResourceId,\n author: AuthorMarker | undefined,\n cb: (p: ProjectMutator) => T | Promise<T>,\n ops: Partial<TxOps> = {},\n): Promise<T> {\n if (txOrPl instanceof PlClient) {\n return await txOrPl.withWriteTx(\n \"ProjectAction\" + (ops.name ? `: ${ops.name}` : \"\"),\n async (tx) => {\n const mut = await ProjectMutator.load(projectHelper, tx, rid, author);\n const result = await cb(mut);\n if (!mut.wasModified)\n // skipping save and commit altogether if no modifications were actually made\n return result;\n mut.save();\n await tx.commit();\n if (getDebugFlags().logProjectMutationStat) console.log(JSON.stringify(tx.stat));\n return result;\n },\n ops,\n );\n } else {\n const mut = await ProjectMutator.load(projectHelper, txOrPl, rid, author);\n const result = await cb(mut);\n mut.save();\n return result;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;AAiGA,SAAS,OAAiB,SAAsB,SAA2B;CACzE,IAAI,cAAc;CAClB,IAAI,YAA+B;CACnC,IAAI,QAAuB;AAC3B,cAAa;AACX,MAAI,CAAC,aAAa;AAChB,iBAAc;AACd,eAAY,SAAS;AACrB,WAAQ,SAAS;AACjB,UAAO;;EAET,MAAM,eAAe,SAAS;AAC9B,MAAI,cAAc,cAAc;AAC9B,eAAY;AACZ,WAAQ,SAAS;;AAEnB,SAAO,SAAS;;;AAIpB,IAAM,YAAN,MAAgB;CACd,YACE,AAAgB,IAChB,AAAgB,QAChB,AAAgB,QAChB,AAAgB,QAChB,AAAiB,SAAmB,IAAI,sBAAsB,EAC9D;EALgB;EACA;EACA;EACA;EACC;;CAGnB,AAAO,QAAQ;AAGb,MAAK,KAAK,OAAO,eAAe,YAAgB,KAAK,OAAO,YAAY,QACtE,OAAM,IAAI,MAAM,2BAA2B;AAE7C,MAAK,KAAK,OAAO,kBAAkB,YAAgB,KAAK,OAAO,eAAe,QAC5E,OAAM,IAAI,MAAM,4BAA4B;AAE9C,MACG,KAAK,OAAO,uBAAuB,YACnC,KAAK,OAAO,oBAAoB,QAEjC,OAAM,IAAI,MAAM,iCAAiC;AAEnD,MACG,KAAK,OAAO,0BAA0B,YACtC,KAAK,OAAO,uBA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"],"sourcesContent":["import type {\n AnyRef,\n AnyResourceRef,\n BasicResourceData,\n PlTransaction,\n ResourceData,\n ResourceId,\n TxOps,\n} from \"@milaboratories/pl-client\";\nimport {\n ensureResourceIdNotNull,\n field,\n isNotNullResourceId,\n isNullResourceId,\n isResource,\n isResourceId,\n isResourceRef,\n Pl,\n PlClient,\n} from \"@milaboratories/pl-client\";\nimport { createRenderHeavyBlock, createBContextFromUpstreams } from \"./template/render_block\";\nimport type {\n Block,\n ProjectStructure,\n ProjectField,\n ProjectRenderingState,\n} from \"../model/project_model\";\nimport {\n BlockRenderingStateKey,\n ProjectStructureKey,\n parseProjectField,\n projectFieldName,\n SchemaVersionCurrent,\n SchemaVersionKey,\n ProjectResourceType,\n InitialBlockStructure,\n InitialProjectRenderingState,\n ProjectMetaKey,\n InitialBlockMeta,\n blockArgsAuthorKey,\n ProjectLastModifiedTimestamp,\n ProjectCreatedTimestamp,\n ProjectStructureAuthorKey,\n getServiceTemplateField,\n FieldsToDuplicate,\n} from \"../model/project_model\";\nimport { BlockPackTemplateField, createBlockPack } from \"./block-pack/block_pack\";\nimport type { BlockGraph, ProductionGraphBlockInfo } from \"../model/project_model_util\";\nimport { allBlocks, graphDiff, productionGraph, stagingGraph } from \"../model/project_model_util\";\nimport type { BlockPackSpecPrepared } from \"../model\";\nimport type {\n AuthorMarker,\n BlockPackSpec,\n BlockSettings,\n ProjectMeta,\n} from \"@milaboratories/pl-model-middle-layer\";\nimport { InitialBlockSettings } from \"@milaboratories/pl-model-middle-layer\";\nimport Denque from \"denque\";\nimport { exportContext, getPreparedExportTemplateEnvelope } from \"./context_export\";\nimport { loadTemplate } from \"./template/template_loading\";\nimport {\n cachedDeserialize,\n notEmpty,\n canonicalJsonBytes,\n cachedDecode,\n type MiLogger,\n ConsoleLoggerAdapter,\n} from \"@milaboratories/ts-helpers\";\nimport type { ProjectHelper } from \"../model/project_helper\";\nimport {\n extractConfig,\n UiError,\n BLOCK_STORAGE_FACADE_VERSION,\n type BlockConfig,\n} from \"@platforma-sdk/model\";\nimport { getDebugFlags } from \"../debug\";\nimport type { BlockPackInfo } from \"../model/block_pack\";\n\ntype FieldStatus = \"NotReady\" | \"Ready\" | \"Error\";\n\ninterface BlockFieldState {\n modCount: number;\n ref?: AnyRef;\n status?: FieldStatus;\n value?: Uint8Array;\n}\n\ntype BlockFieldStates = Partial<Record<ProjectField[\"fieldName\"], BlockFieldState>>;\ntype BlockFieldStateValue = Omit<BlockFieldState, \"modCount\">;\n\ninterface BlockInfoState {\n readonly id: string;\n readonly fields: BlockFieldStates;\n blockConfig?: BlockConfig;\n blockPack?: BlockPackSpec;\n}\n\nfunction cached<ModId, T>(modIdCb: () => ModId, valueCb: () => T): () => T {\n let initialized = false;\n let lastModId: ModId | undefined = undefined;\n let value: T | undefined = undefined;\n return () => {\n if (!initialized) {\n initialized = true;\n lastModId = modIdCb();\n value = valueCb();\n return value;\n }\n const currentModId = modIdCb();\n if (lastModId !== currentModId) {\n lastModId = currentModId;\n value = valueCb();\n }\n return valueCb();\n };\n}\n\nclass BlockInfo {\n constructor(\n public readonly id: string,\n public readonly fields: BlockFieldStates,\n public readonly config: BlockConfig,\n public readonly source: BlockPackSpec,\n private readonly logger: MiLogger = new ConsoleLoggerAdapter(),\n ) {}\n\n public check() {\n // state assertions\n\n if ((this.fields.prodOutput === undefined) !== (this.fields.prodCtx === undefined))\n throw new Error(\"inconsistent prod fields\");\n\n if ((this.fields.stagingOutput === undefined) !== (this.fields.stagingCtx === undefined))\n throw new Error(\"inconsistent stage fields\");\n\n if (\n (this.fields.prodOutputPrevious === undefined) !==\n (this.fields.prodCtxPrevious === undefined)\n )\n throw new Error(\"inconsistent prod cache fields\");\n\n if (\n (this.fields.stagingOutputPrevious === undefined) !==\n (this.fields.stagingCtxPrevious === undefined)\n )\n throw new Error(\"inconsistent stage cache fields\");\n\n if (this.fields.blockPack === undefined) throw new Error(\"no block pack field\");\n\n if (this.fields.blockStorage === undefined) throw new Error(\"no block storage field\");\n }\n\n private readonly currentArgsC = cached(\n () => this.fields.currentArgs?.modCount,\n () => {\n const bin = this.fields.currentArgs?.value;\n if (bin === undefined) return undefined;\n return cachedDeserialize(bin);\n },\n );\n\n private readonly blockStorageC = cached(\n () => this.fields.blockStorage!.modCount,\n () => {\n const bin = this.fields.blockStorage?.value;\n if (bin === undefined) return undefined;\n return cachedDeserialize<Record<string, unknown>>(bin);\n },\n );\n\n private readonly blockStorageJ = cached(\n () => this.fields.blockStorage!.modCount,\n () => {\n const bin = this.fields.blockStorage?.value;\n if (bin === undefined) return undefined;\n return cachedDecode(bin);\n },\n );\n\n private readonly prodArgsC = cached(\n () => this.fields.prodArgs?.modCount,\n () => {\n const bin = this.fields.prodArgs?.value;\n if (bin === undefined) return undefined;\n return cachedDeserialize(bin);\n },\n );\n\n private readonly currentPrerunArgsC = cached(\n () => this.fields.currentPrerunArgs?.modCount,\n () => {\n const bin = this.fields.currentPrerunArgs?.value;\n if (bin === undefined) return undefined;\n return cachedDeserialize(bin);\n },\n );\n\n get currentArgs(): unknown {\n return this.currentArgsC();\n }\n\n get blockStorage(): unknown {\n try {\n return this.blockStorageC();\n } catch (e) {\n this.logger.error(new Error(`Error getting blockStorage for ${this.id}`, { cause: e }));\n return undefined;\n }\n }\n\n get blockStorageJson() {\n return this.blockStorageJ();\n }\n\n get currentPrerunArgs(): unknown {\n return this.currentPrerunArgsC();\n }\n\n get stagingRendered(): boolean {\n return this.fields.stagingCtx !== undefined;\n }\n\n get productionRendered(): boolean {\n return this.fields.prodCtx !== undefined;\n }\n\n get productionHasErrors(): boolean {\n return this.fields.prodUiCtx?.status === \"Error\";\n }\n\n private readonly productionStaleC: () => boolean = cached(\n () => `${this.fields.currentArgs!.modCount}_${this.fields.prodArgs?.modCount}`,\n () =>\n this.fields.prodArgs === undefined ||\n Buffer.compare(this.fields.currentArgs!.value!, this.fields.prodArgs.value!) !== 0,\n );\n\n get requireProductionRendering(): boolean {\n return !this.productionRendered || this.productionStaleC() || this.productionHasErrors;\n }\n\n /** Returns true if staging should be re-rendered (stagingCtx is not set) */\n get requireStagingRendering(): boolean {\n // No staging needed if currentPrerunArgs is undefined (args derivation failed)\n if (this.fields.currentPrerunArgs === undefined) return false;\n return !this.stagingRendered;\n }\n\n get prodArgs(): unknown {\n return this.prodArgsC();\n }\n\n public getTemplate(tx: PlTransaction): AnyRef {\n return tx.getFutureFieldValue(\n Pl.unwrapHolder(tx, this.fields.blockPack!.ref!),\n BlockPackTemplateField,\n \"Input\",\n );\n }\n}\n\n/**\n * Specification for creating a new block.\n * Discriminated union based on `storageMode`.\n */\n/** Specification for creating a new block. Discriminated union based on `storageMode`. */\nexport type NewBlockSpec =\n | { storageMode: \"fromModel\"; blockPack: BlockPackSpecPrepared }\n | { storageMode: \"legacy\"; blockPack: BlockPackSpecPrepared; legacyState: string };\n\nconst NoNewBlocks = (blockId: string) => {\n throw new Error(`No new block info for ${blockId}`);\n};\n\n/**\n * Request to set block state using unified state format.\n * For v3 blocks: state is the block's state\n * For v1/v2 blocks: state should be { args, uiState } format\n */\nexport type SetStatesRequest =\n | {\n blockId: string;\n /** The unified state to set */\n state: unknown;\n modelAPIVersion: 1;\n }\n | {\n blockId: string;\n /** Storage operation payload - middle layer is agnostic to specific operations */\n payload: { operation: string; value: unknown };\n modelAPIVersion: 2;\n };\n\nexport type ClearState = {\n state: unknown;\n};\n\nexport class ProjectMutator {\n private globalModCount = 0;\n private fieldsChanged: boolean = false;\n\n //\n // Change trackers\n //\n\n private lastModifiedChanged = false;\n private structureChanged = false;\n private metaChanged = false;\n private renderingStateChanged = false;\n\n /** Set blocks will be assigned current mutator author marker on save */\n private readonly blocksWithChangedInputs = new Set<string>();\n\n constructor(\n public readonly rid: ResourceId,\n private readonly tx: PlTransaction,\n private readonly author: AuthorMarker | undefined,\n private readonly schema: string,\n private lastModified: number,\n private meta: ProjectMeta,\n private struct: ProjectStructure,\n private readonly renderingState: Omit<ProjectRenderingState, \"blocksInLimbo\">,\n private readonly blocksInLimbo: Set<string>,\n private readonly blockInfos: Map<string, BlockInfo>,\n private readonly ctxExportTplHolder: AnyResourceRef,\n private readonly projectHelper: ProjectHelper,\n ) {}\n\n private fixProblemsAndMigrate() {\n // Fix inconsistent production fields\n this.blockInfos.forEach((blockInfo) => {\n if (\n blockInfo.fields.prodArgs === undefined ||\n blockInfo.fields.prodOutput === undefined ||\n blockInfo.fields.prodCtx === undefined\n )\n this.deleteBlockFields(blockInfo.id, \"prodArgs\", \"prodOutput\", \"prodCtx\", \"prodUiCtx\");\n });\n\n // Fix inconsistent staging fields\n this.blockInfos.forEach((blockInfo) => {\n if (blockInfo.fields.stagingOutput === undefined || blockInfo.fields.stagingCtx === undefined)\n this.deleteBlockFields(blockInfo.id, \"stagingOutput\", \"stagingCtx\", \"stagingUiCtx\");\n });\n\n // Fix inconsistent cache fields\n this.blockInfos.forEach((blockInfo) => {\n if (\n blockInfo.fields.prodOutputPrevious === undefined ||\n blockInfo.fields.prodCtxPrevious === undefined\n )\n this.deleteBlockFields(\n blockInfo.id,\n \"prodOutputPrevious\",\n \"prodCtxPrevious\",\n \"prodUiCtxPrevious\",\n );\n if (\n blockInfo.fields.stagingOutputPrevious === undefined ||\n blockInfo.fields.stagingCtxPrevious === undefined\n )\n this.deleteBlockFields(\n blockInfo.id,\n \"stagingOutputPrevious\",\n \"stagingCtxPrevious\",\n \"stagingUiCtxPrevious\",\n );\n });\n\n // Migration for addition of block settings field\n let initialBlockSettings: Omit<BlockFieldState, \"modCount\"> | undefined;\n this.blockInfos.forEach((blockInfo) => {\n if (blockInfo.fields.blockSettings === undefined) {\n if (initialBlockSettings === undefined)\n initialBlockSettings = this.createJsonFieldValue(InitialBlockSettings);\n this.setBlockFieldObj(blockInfo.id, \"blockSettings\", initialBlockSettings);\n }\n });\n\n // Validate after fixes\n this.blockInfos.forEach((info) => info.check());\n }\n\n get wasModified(): boolean {\n return (\n this.lastModifiedChanged ||\n this.structureChanged ||\n this.fieldsChanged ||\n this.metaChanged ||\n this.renderingStateChanged\n );\n }\n\n get structure(): ProjectStructure {\n return JSON.parse(JSON.stringify(this.struct)) as ProjectStructure;\n }\n\n //\n // Graph calculation\n //\n\n private stagingGraph: BlockGraph | undefined = undefined;\n private pendingProductionGraph: BlockGraph | undefined = undefined;\n private actualProductionGraph: BlockGraph | undefined = undefined;\n\n private getStagingGraph(): BlockGraph {\n if (this.stagingGraph === undefined) this.stagingGraph = stagingGraph(this.struct);\n return this.stagingGraph;\n }\n\n private getProductionGraphBlockInfo(\n blockId: string,\n prod: boolean,\n ): ProductionGraphBlockInfo | undefined {\n const bInfo = this.getBlockInfo(blockId);\n\n let argsField: BlockFieldState | undefined;\n let args: unknown;\n\n if (prod) {\n if (bInfo.fields.prodArgs === undefined) return undefined;\n argsField = bInfo.fields.prodArgs;\n args = bInfo.prodArgs;\n } else {\n argsField = bInfo.fields.currentArgs;\n args = bInfo.currentArgs;\n }\n\n // Can't compute enrichment targets without args\n if (argsField === undefined) {\n return { args, enrichmentTargets: undefined };\n }\n\n const blockPackField = notEmpty(bInfo.fields.blockPack);\n\n if (isResourceId(argsField.ref!) && isResourceId(blockPackField.ref!))\n return {\n args,\n enrichmentTargets: this.projectHelper.getEnrichmentTargets(\n () => bInfo.config,\n () => args,\n { argsRid: argsField.ref, blockPackRid: blockPackField.ref },\n ),\n };\n else\n return {\n args,\n enrichmentTargets: this.projectHelper.getEnrichmentTargets(\n () => bInfo.config,\n () => args,\n ),\n };\n }\n\n private getPendingProductionGraph(): BlockGraph {\n if (this.pendingProductionGraph === undefined)\n this.pendingProductionGraph = productionGraph(this.struct, (blockId) =>\n this.getProductionGraphBlockInfo(blockId, false),\n );\n return this.pendingProductionGraph;\n }\n\n private getActualProductionGraph(): BlockGraph {\n if (this.actualProductionGraph === undefined)\n this.actualProductionGraph = productionGraph(this.struct, (blockId) =>\n this.getProductionGraphBlockInfo(blockId, true),\n );\n return this.actualProductionGraph;\n }\n\n //\n // Generic helpers to interact with project state\n //\n\n private getBlockInfo(blockId: string): BlockInfo {\n const info = this.blockInfos.get(blockId);\n if (info === undefined) throw new Error(`No such block: ${blockId}`);\n return info;\n }\n\n private createJsonFieldValueByContent(content: string): BlockFieldStateValue {\n if (content === undefined) throw new Error(\"content is undefined\");\n const value = Buffer.from(content);\n const ref = this.tx.createValue(Pl.JsonObject, value);\n return { ref, value, status: \"Ready\" };\n }\n\n private createJsonFieldValue(obj: unknown): BlockFieldStateValue {\n return this.createJsonFieldValueByContent(JSON.stringify(obj));\n }\n\n private getBlock(blockId: string): Block {\n for (const block of allBlocks(this.struct)) if (block.id === blockId) return block;\n throw new Error(\"block not found\");\n }\n\n private setBlockFieldObj(\n blockId: string,\n fieldName: keyof BlockFieldStates,\n state: BlockFieldStateValue,\n ) {\n const fid = field(this.rid, projectFieldName(blockId, fieldName));\n\n if (state.ref === undefined) throw new Error(\"Can't set value with empty ref\");\n\n if (this.getBlockInfo(blockId).fields[fieldName] === undefined)\n this.tx.createField(fid, \"Dynamic\", state.ref);\n else this.tx.setField(fid, state.ref);\n\n this.getBlockInfo(blockId).fields[fieldName] = {\n modCount: this.globalModCount++,\n ...state,\n };\n\n this.fieldsChanged = true;\n }\n\n private setBlockField(\n blockId: string,\n fieldName: keyof BlockFieldStates,\n ref: AnyRef,\n status: FieldStatus,\n value?: Uint8Array,\n ) {\n this.setBlockFieldObj(blockId, fieldName, { ref, status, value });\n }\n\n private deleteBlockFields(blockId: string, ...fieldNames: (keyof BlockFieldStates)[]): boolean {\n let deleted = false;\n const info = this.getBlockInfo(blockId);\n for (const fieldName of fieldNames) {\n const fields = info.fields;\n if (!(fieldName in fields)) continue;\n this.tx.removeField(field(this.rid, projectFieldName(blockId, fieldName)));\n delete fields[fieldName];\n this.fieldsChanged = true;\n deleted = true;\n }\n return deleted;\n }\n\n private updateLastModified() {\n this.lastModified = Date.now();\n this.lastModifiedChanged = true;\n }\n\n //\n // Main project actions\n //\n\n private resetStagingRefreshTimestamp() {\n this.renderingState.stagingRefreshTimestamp = Date.now();\n this.renderingStateChanged = true;\n }\n\n private resetStaging(blockId: string): void {\n const fields = this.getBlockInfo(blockId).fields;\n if (\n fields.stagingOutput?.status === \"Ready\" &&\n fields.stagingCtx?.status === \"Ready\" &&\n fields.stagingUiCtx?.status === \"Ready\"\n ) {\n this.setBlockFieldObj(blockId, \"stagingOutputPrevious\", fields.stagingOutput);\n this.setBlockFieldObj(blockId, \"stagingCtxPrevious\", fields.stagingCtx);\n this.setBlockFieldObj(blockId, \"stagingUiCtxPrevious\", fields.stagingUiCtx);\n }\n if (this.deleteBlockFields(blockId, \"stagingOutput\", \"stagingCtx\", \"stagingUiCtx\"))\n this.resetStagingRefreshTimestamp();\n }\n\n private resetProduction(blockId: string): void {\n const fields = this.getBlockInfo(blockId).fields;\n if (\n fields.prodOutput?.status === \"Ready\" &&\n fields.prodCtx?.status === \"Ready\" &&\n fields.prodUiCtx?.status === \"Ready\"\n ) {\n this.setBlockFieldObj(blockId, \"prodOutputPrevious\", fields.prodOutput);\n this.setBlockFieldObj(blockId, \"prodCtxPrevious\", fields.prodCtx);\n this.setBlockFieldObj(blockId, \"prodUiCtxPrevious\", fields.prodUiCtx);\n }\n this.deleteBlockFields(blockId, \"prodOutput\", \"prodCtx\", \"prodUiCtx\", \"prodArgs\");\n }\n\n /** Running blocks are reset, already computed moved to limbo. Returns if\n * either of the actions were actually performed.\n * This method ensures the block is left in a consistent state that passes check() constraints. */\n private resetOrLimboProduction(blockId: string): boolean {\n const fields = this.getBlockInfo(blockId).fields;\n\n // Check if we can safely move to limbo (both core production fields are ready)\n if (fields.prodOutput?.status === \"Ready\" && fields.prodCtx?.status === \"Ready\") {\n if (this.blocksInLimbo.has(blockId))\n // we are already in limbo\n return false;\n\n // limbo - keep the ready production results but clean up cache\n this.blocksInLimbo.add(blockId);\n this.renderingStateChanged = true;\n\n // doing some gc - clean up previous cache fields\n this.deleteBlockFields(blockId, \"prodOutputPrevious\", \"prodCtxPrevious\", \"prodUiCtxPrevious\");\n\n return true;\n } else {\n // reset - clean up any partial/inconsistent production stat\n return this.deleteBlockFields(\n blockId,\n \"prodOutput\",\n \"prodCtx\",\n \"prodUiCtx\",\n \"prodArgs\",\n \"prodOutputPrevious\",\n \"prodCtxPrevious\",\n \"prodUiCtxPrevious\",\n );\n }\n }\n\n /**\n * Gets current block state and merges with partial updates.\n * Used by legacy v1/v2 methods like setBlockArgs and setUiState.\n *\n * @param blockId The block to get state for\n * @param partialUpdate Partial state to merge (e.g. { args } or { uiState })\n * @returns Merged state in unified format { args, uiState }\n */\n public mergeBlockState(\n blockId: string,\n partialUpdate: { args?: unknown; uiState?: unknown },\n ): { args?: unknown; uiState?: unknown } {\n const info = this.getBlockInfo(blockId);\n const currentState = info.blockStorage as { args?: unknown; uiState?: unknown } | undefined;\n if (currentState === undefined) {\n throw new Error(`Cannot merge block state for ${blockId}: blockStorage is unavailable`);\n }\n return { ...currentState, ...partialUpdate };\n }\n\n /**\n * Sets raw block storage content directly (for testing purposes).\n * This bypasses all normalization and VM transformations.\n *\n * @param blockId The block to set storage for\n * @param rawStorageJson Raw storage as JSON string\n */\n public setBlockStorageRaw(blockId: string, rawStorageJson: string): void {\n this.setBlockFieldObj(\n blockId,\n \"blockStorage\",\n this.createJsonFieldValueByContent(rawStorageJson),\n );\n this.blocksWithChangedInputs.add(blockId);\n this.updateLastModified();\n }\n\n /**\n * Resets a v2+ block to its initial storage state.\n * Gets initial storage from VM and derives args from it.\n *\n * For v1 blocks, use setStates() instead.\n *\n * @param blockId The block to reset\n */\n public resetToInitialStorage(blockId: string): void {\n const info = this.getBlockInfo(blockId);\n const blockConfig = info.config;\n\n if (blockConfig.modelAPIVersion !== BLOCK_STORAGE_FACADE_VERSION) {\n throw new Error(\"resetToInitialStorage is only supported for model API version 2\");\n }\n\n // Get initial storage from VM\n const initialStorageJson = this.projectHelper.getInitialStorageInVM(blockConfig);\n this.setBlockStorageRaw(blockId, initialStorageJson);\n\n // Derive args from storage - set or clear currentArgs based on derivation result\n const deriveArgsResult = this.projectHelper.deriveArgsFromStorage(\n blockConfig,\n initialStorageJson,\n );\n if (!deriveArgsResult.error) {\n this.setBlockFieldObj(\n blockId,\n \"currentArgs\",\n this.createJsonFieldValue(deriveArgsResult.value),\n );\n // Derive prerunArgs from storage\n const prerunArgs = this.projectHelper.derivePrerunArgsFromStorage(\n blockConfig,\n initialStorageJson,\n );\n if (prerunArgs !== undefined) {\n this.setBlockFieldObj(blockId, \"currentPrerunArgs\", this.createJsonFieldValue(prerunArgs));\n } else {\n this.deleteBlockFields(blockId, \"currentPrerunArgs\");\n }\n } else {\n if (info.fields.currentPrerunArgs !== undefined) {\n this.projectHelper.logger.warn(\n `[staging] ${blockId}: currentPrerunArgs cleared (args derivation failed)`,\n );\n }\n this.deleteBlockFields(blockId, \"currentArgs\");\n this.deleteBlockFields(blockId, \"currentPrerunArgs\");\n }\n }\n\n /** Optimally sets inputs for multiple blocks in one go */\n public setStates(requests: SetStatesRequest[]) {\n const changedArgs: string[] = [];\n let somethingChanged = false;\n for (const req of requests) {\n const info = this.getBlockInfo(req.blockId);\n let blockChanged = false;\n\n const blockConfig = info.config;\n // modelAPIVersion === 2 means BlockModelV3 with .args() lambda for deriving args\n\n if (req.modelAPIVersion !== blockConfig.modelAPIVersion) {\n throw new Error(\n `Model API version mismatch for block ${req.blockId}: ${req.modelAPIVersion} !== ${blockConfig.modelAPIVersion}`,\n );\n }\n\n // Derive args from storage using the block's config.args() callback\n let args: unknown;\n let prerunArgs: unknown;\n\n if (req.modelAPIVersion === BLOCK_STORAGE_FACADE_VERSION) {\n const currentStorageJson = info.blockStorageJson;\n if (currentStorageJson === undefined) {\n throw new Error(`Block ${req.blockId} has no blockStorage - this should not happen`);\n }\n\n // Apply the state update to storage\n const updatedStorageJson = this.projectHelper.applyStorageUpdateInVM(\n blockConfig,\n currentStorageJson,\n req.payload,\n );\n\n this.setBlockFieldObj(\n req.blockId,\n \"blockStorage\",\n this.createJsonFieldValueByContent(updatedStorageJson),\n );\n\n // Derive args directly from storage (VM extracts data internally)\n const derivedArgsResult = this.projectHelper.deriveArgsFromStorage(\n blockConfig,\n updatedStorageJson,\n );\n if (derivedArgsResult.error) {\n args = undefined;\n prerunArgs = undefined;\n } else {\n args = derivedArgsResult.value;\n // Derive prerunArgs from storage, or fall back to args\n prerunArgs = this.projectHelper.derivePrerunArgsFromStorage(\n blockConfig,\n updatedStorageJson,\n );\n }\n } else {\n this.setBlockFieldObj(req.blockId, \"blockStorage\", this.createJsonFieldValue(req.state));\n if (req.state !== null && typeof req.state === \"object\" && \"args\" in req.state) {\n args = (req.state as { args: unknown }).args;\n } else {\n args = req.state;\n }\n // For the legacy blocks, prerunArgs = args (same as production args)\n prerunArgs = args;\n }\n\n // Set or clear currentArgs based on derivation result\n if (args !== undefined) {\n const currentArgsData = canonicalJsonBytes(args);\n const argsPartRef = this.tx.createValue(Pl.JsonObject, currentArgsData);\n this.setBlockField(req.blockId, \"currentArgs\", argsPartRef, \"Ready\", currentArgsData);\n } else {\n this.deleteBlockFields(req.blockId, \"currentArgs\");\n }\n\n // Set currentPrerunArgs field and check if it actually changed\n let prerunArgsChanged = false;\n if (prerunArgs !== undefined) {\n const prerunArgsData = canonicalJsonBytes(prerunArgs);\n const oldPrerunArgsData = info.fields.currentPrerunArgs?.value;\n // Check if prerunArgs actually changed\n if (\n oldPrerunArgsData === undefined ||\n Buffer.compare(oldPrerunArgsData, prerunArgsData) !== 0\n ) {\n prerunArgsChanged = true;\n }\n const prerunArgsRef = this.tx.createValue(Pl.JsonObject, prerunArgsData);\n this.setBlockField(\n req.blockId,\n \"currentPrerunArgs\",\n prerunArgsRef,\n \"Ready\",\n prerunArgsData,\n );\n } else {\n this.deleteBlockFields(req.blockId, \"currentPrerunArgs\");\n }\n\n blockChanged = true;\n // Only add to changedArgs if prerunArgs changed - this controls staging reset\n if (prerunArgsChanged) {\n changedArgs.push(req.blockId);\n }\n\n if (blockChanged) {\n // will be assigned our author marker\n this.blocksWithChangedInputs.add(req.blockId);\n somethingChanged = true;\n }\n }\n\n // resetting staging outputs for all downstream blocks\n this.getStagingGraph().traverse(\"downstream\", changedArgs, ({ id }) => this.resetStaging(id));\n\n if (somethingChanged) this.updateLastModified();\n }\n\n public setBlockSettings(blockId: string, newValue: BlockSettings): void {\n this.setBlockFieldObj(blockId, \"blockSettings\", this.createJsonFieldValue(newValue));\n this.updateLastModified();\n }\n\n private createProdCtx(upstream: Set<string>): AnyRef {\n const upstreamContexts: AnyRef[] = [];\n upstream.forEach((id) => {\n const info = this.getBlockInfo(id);\n if (info.fields[\"prodCtx\"]?.ref === undefined)\n throw new Error(\"One of the upstreams staging is not rendered.\");\n upstreamContexts.push(Pl.unwrapHolder(this.tx, info.fields[\"prodCtx\"].ref));\n });\n return createBContextFromUpstreams(this.tx, upstreamContexts);\n }\n\n private createStagingCtx(upstream: Set<string>): AnyRef {\n const upstreamContexts: AnyRef[] = [];\n upstream.forEach((id) => {\n const info = this.getBlockInfo(id);\n if (info.fields[\"stagingCtx\"]?.ref !== undefined) {\n upstreamContexts.push(Pl.unwrapHolder(this.tx, info.fields[\"stagingCtx\"].ref));\n } else if (info.fields.currentPrerunArgs !== undefined) {\n // Upstream has currentPrerunArgs but no staging — this is an inconsistency\n throw new Error(`Upstream ${id} staging is not rendered but has currentPrerunArgs set.`);\n }\n // Blocks without currentPrerunArgs (e.g. block model doesn't define prerunArgs, or args\n // derivation failed) never get stagingCtx. Use prodCtx if available.\n if (info.fields[\"prodCtx\"]?.ref !== undefined)\n upstreamContexts.push(Pl.unwrapHolder(this.tx, info.fields[\"prodCtx\"].ref));\n });\n return createBContextFromUpstreams(this.tx, upstreamContexts);\n }\n\n private exportCtx(ctx: AnyRef): AnyRef {\n return exportContext(this.tx, Pl.unwrapHolder(this.tx, this.ctxExportTplHolder), ctx);\n }\n\n /**\n * Renders staging for a block using currentPrerunArgs.\n * If currentPrerunArgs is not set (prerunArgs returned undefined), skips staging for this block.\n */\n private renderStagingFor(blockId: string) {\n const info = this.getBlockInfo(blockId);\n\n // Check BEFORE resetStaging: if currentPrerunArgs is not set (e.g. prerunArgs() returned undefined\n // because inputs aren't ready, or args derivation failed), skip without clearing existing staging.\n // Otherwise resetStaging would delete stagingCtx, and downstream blocks that reference this block\n // as an upstream would fail in createStagingCtx.\n const prerunArgsRef = info.fields.currentPrerunArgs?.ref;\n if (prerunArgsRef === undefined) {\n return;\n }\n\n this.resetStaging(blockId);\n\n const ctx = this.createStagingCtx(this.getStagingGraph().nodes.get(blockId)!.upstream);\n\n if (this.getBlock(blockId).renderingMode !== \"Heavy\") throw new Error(\"not supported yet\");\n\n const tpl = info.getTemplate(this.tx);\n\n // Use currentPrerunArgs for staging rendering\n const results = createRenderHeavyBlock(this.tx, tpl, {\n args: prerunArgsRef,\n blockId: this.tx.createValue(Pl.JsonString, JSON.stringify(blockId)),\n isProduction: this.tx.createValue(Pl.JsonBool, JSON.stringify(false)),\n context: ctx,\n });\n\n // Here we set the staging ctx to the input context of the staging workflow, not the output because exports\n // of one staging context should stay within the same block, and not travel downstream.\n // We may change this decision in the future if wanted to support traveling staging exports downstream.\n this.setBlockField(blockId, \"stagingCtx\", Pl.wrapInEphHolder(this.tx, ctx), \"NotReady\");\n\n // Yet the staging UI Ctx is the output context of the staging workflow, because it is used to form the result pool\n // thus creating a certain discrepancy between staging workflow context behavior and desktop's result pool.\n this.setBlockField(blockId, \"stagingUiCtx\", this.exportCtx(results.context), \"NotReady\");\n this.setBlockField(blockId, \"stagingOutput\", results.result, \"NotReady\");\n }\n\n private renderProductionFor(blockId: string) {\n this.resetProduction(blockId);\n\n const info = this.getBlockInfo(blockId);\n\n // Can't render production if currentArgs is not set\n if (info.fields.currentArgs === undefined) {\n throw new Error(`Can't render production for block ${blockId}: currentArgs not set`);\n }\n\n const ctx = this.createProdCtx(this.getPendingProductionGraph().nodes.get(blockId)!.upstream);\n\n if (this.getBlock(blockId).renderingMode === \"Light\")\n throw new Error(\"Can't render production for light block.\");\n\n const tpl = info.getTemplate(this.tx);\n\n const results = createRenderHeavyBlock(this.tx, tpl, {\n args: info.fields.currentArgs.ref!,\n blockId: this.tx.createValue(Pl.JsonString, JSON.stringify(blockId)),\n isProduction: this.tx.createValue(Pl.JsonBool, JSON.stringify(true)),\n context: ctx,\n });\n this.setBlockField(\n blockId,\n \"prodCtx\",\n Pl.wrapInEphHolder(this.tx, results.context),\n \"NotReady\",\n );\n this.setBlockField(blockId, \"prodUiCtx\", this.exportCtx(results.context), \"NotReady\");\n this.setBlockField(blockId, \"prodOutput\", results.result, \"NotReady\");\n\n // saving inputs for which we rendered the production\n this.setBlockFieldObj(blockId, \"prodArgs\", info.fields.currentArgs);\n\n // removing block from limbo as we juts rendered fresh production for it\n if (this.blocksInLimbo.delete(blockId)) this.renderingStateChanged = true;\n }\n\n //\n // Structure changes\n //\n\n private initializeNewBlock(blockId: string, spec: NewBlockSpec): void {\n const info = new BlockInfo(\n blockId,\n {},\n extractConfig(spec.blockPack.config),\n spec.blockPack.source,\n this.projectHelper.logger,\n );\n this.blockInfos.set(blockId, info);\n\n // block pack\n const bp = createBlockPack(this.tx, spec.blockPack);\n this.setBlockField(blockId, \"blockPack\", Pl.wrapInHolder(this.tx, bp), \"NotReady\");\n\n // settings\n this.setBlockFieldObj(\n blockId,\n \"blockSettings\",\n this.createJsonFieldValue(InitialBlockSettings),\n );\n\n const blockConfig = info.config;\n\n let args: unknown;\n let prerunArgs: unknown;\n let storageToWrite: string;\n\n if (spec.storageMode === \"fromModel\") {\n // Model API v2+: get initial storage and derive args from it\n storageToWrite = this.projectHelper.getInitialStorageInVM(blockConfig);\n\n // Derive args directly from storage (VM extracts data internally)\n const deriveArgsResult = this.projectHelper.deriveArgsFromStorage(\n blockConfig,\n storageToWrite,\n );\n if (deriveArgsResult.error) {\n args = undefined;\n prerunArgs = undefined;\n } else {\n args = deriveArgsResult.value;\n prerunArgs = this.projectHelper.derivePrerunArgsFromStorage(blockConfig, storageToWrite);\n }\n } else if (spec.storageMode === \"legacy\") {\n // Model API v1: use legacyState from spec\n const parsedState = JSON.parse(spec.legacyState);\n args = parsedState.args;\n if (args === undefined) {\n throw new Error(\"args is undefined in legacyState\");\n }\n prerunArgs = args;\n storageToWrite = spec.legacyState;\n } else {\n throw new Error(`Unknown storageMode: ${(spec as NewBlockSpec).storageMode}`);\n }\n\n // currentArgs\n if (args !== undefined) {\n this.setBlockFieldObj(blockId, \"currentArgs\", this.createJsonFieldValue(args));\n }\n\n // currentPrerunArgs\n if (prerunArgs !== undefined) {\n this.setBlockFieldObj(blockId, \"currentPrerunArgs\", this.createJsonFieldValue(prerunArgs));\n }\n\n // blockStorage\n this.setBlockFieldObj(\n blockId,\n \"blockStorage\",\n this.createJsonFieldValueByContent(storageToWrite),\n );\n\n // checking structure\n info.check();\n }\n\n private getFieldNamesToDuplicate(blockId: string): Set<ProjectField[\"fieldName\"]> {\n const fields = this.getBlockInfo(blockId).fields;\n\n const diff = <T>(setA: Set<T>, setB: Set<T>): Set<T> =>\n new Set([...setA].filter((x) => !setB.has(x)));\n\n // Check if we can safely move to limbo (both core production fields are ready)\n if (fields.prodOutput?.status === \"Ready\" && fields.prodCtx?.status === \"Ready\") {\n if (this.blocksInLimbo.has(blockId))\n // we are already in limbo\n return FieldsToDuplicate;\n\n return diff(\n FieldsToDuplicate,\n new Set([\"prodOutputPrevious\", \"prodCtxPrevious\", \"prodUiCtxPrevious\"]),\n );\n } else {\n return diff(\n FieldsToDuplicate,\n new Set([\n \"prodOutput\",\n \"prodCtx\",\n \"prodUiCtx\",\n \"prodArgs\",\n \"prodOutputPrevious\",\n \"prodCtxPrevious\",\n \"prodUiCtxPrevious\",\n ]),\n );\n }\n }\n\n private initializeBlockDuplicate(blockId: string, originalBlockInfo: BlockInfo) {\n const info = new BlockInfo(\n blockId,\n {},\n originalBlockInfo.config,\n originalBlockInfo.source,\n this.projectHelper.logger,\n );\n\n this.blockInfos.set(blockId, info);\n\n const fieldNamesToDuplicate = this.getFieldNamesToDuplicate(blockId);\n\n // Copy all fields from original block to new block by sharing references\n for (const [fieldName, fieldState] of Object.entries(originalBlockInfo.fields)) {\n if (\n fieldNamesToDuplicate.has(fieldName as ProjectField[\"fieldName\"]) &&\n fieldState &&\n fieldState.ref\n ) {\n this.setBlockFieldObj(blockId, fieldName as keyof BlockFieldStates, {\n ref: fieldState.ref,\n status: fieldState.status,\n value: fieldState.value,\n });\n }\n }\n\n this.resetOrLimboProduction(blockId);\n\n info.check();\n }\n\n /** Very generic method, better check for more specialized case-specific methods first. */\n public updateStructure(\n newStructure: ProjectStructure,\n newBlockInitializer: (blockId: string) => void = NoNewBlocks,\n ): void {\n const currentStagingGraph = this.getStagingGraph();\n const currentActualProductionGraph = this.getActualProductionGraph();\n\n const newStagingGraph = stagingGraph(newStructure);\n\n const stagingDiff = graphDiff(currentStagingGraph, newStagingGraph);\n\n // removing blocks\n for (const blockId of stagingDiff.onlyInA) {\n const { fields } = this.getBlockInfo(blockId);\n this.deleteBlockFields(blockId, ...(Object.keys(fields) as ProjectField[\"fieldName\"][]));\n this.blockInfos.delete(blockId);\n if (this.blocksInLimbo.delete(blockId)) this.renderingStateChanged = true;\n }\n\n // creating new blocks\n for (const blockId of stagingDiff.onlyInB) {\n newBlockInitializer(blockId);\n }\n\n // resetting stagings affected by topology change\n for (const blockId of stagingDiff.different) this.resetStaging(blockId);\n\n // new actual production graph without new blocks\n const newActualProductionGraph = productionGraph(newStructure, (blockId) =>\n this.getProductionGraphBlockInfo(blockId, true),\n );\n\n const prodDiff = graphDiff(currentActualProductionGraph, newActualProductionGraph);\n\n // applying changes due to topology change in production to affected nodes and\n // all their downstreams\n currentActualProductionGraph.traverse(\"downstream\", [...prodDiff.different], (node) => {\n this.resetOrLimboProduction(node.id);\n });\n\n if (\n stagingDiff.onlyInB.size > 0 ||\n stagingDiff.onlyInA.size > 0 ||\n stagingDiff.different.size > 0\n )\n this.resetStagingRefreshTimestamp();\n\n this.struct = newStructure;\n this.structureChanged = true;\n this.stagingGraph = undefined;\n this.pendingProductionGraph = undefined;\n this.actualProductionGraph = undefined;\n\n this.updateLastModified();\n }\n\n //\n // Structure change helpers\n //\n\n public addBlock(block: Block, spec: NewBlockSpec, before?: string): void {\n const newStruct = this.structure; // copy current structure\n if (before === undefined) {\n // adding as a very last block\n newStruct.groups[newStruct.groups.length - 1].blocks.push(block);\n } else {\n let done = false;\n for (const group of newStruct.groups) {\n const idx = group.blocks.findIndex((b) => b.id === before);\n if (idx < 0) continue;\n group.blocks.splice(idx, 0, block);\n done = true;\n break;\n }\n if (!done) throw new Error(`Can't find element with id: ${before}`);\n }\n this.updateStructure(newStruct, (blockId) => {\n if (blockId !== block.id) throw new Error(\"Unexpected\");\n this.initializeNewBlock(blockId, spec);\n });\n }\n\n /**\n * Duplicates an existing block by copying all its fields and structure.\n * This method creates a deep copy of the block at the mutator level.\n *\n * @param originalBlockId id of the block to duplicate\n * @param newBlockId id for the new duplicated block\n * @param after id of the block to insert new block after\n */\n public duplicateBlock(originalBlockId: string, newBlockId: string, after?: string): void {\n // Get the original block from structure\n const originalBlock = this.getBlock(originalBlockId);\n const originalBlockInfo = this.getBlockInfo(originalBlockId);\n\n // Create new block in structure\n const newBlock: Block = {\n id: newBlockId,\n label: originalBlock.label,\n renderingMode: originalBlock.renderingMode,\n };\n\n // Add the new block to structure\n const newStruct = this.structure; // copy current structure\n if (after === undefined) {\n // adding as a very last block\n newStruct.groups[newStruct.groups.length - 1].blocks.push(newBlock);\n } else {\n let done = false;\n for (const group of newStruct.groups) {\n const idx = group.blocks.findIndex((b) => b.id === after);\n if (idx < 0) continue;\n group.blocks.splice(idx + 1, 0, newBlock);\n done = true;\n break;\n }\n if (!done) throw new Error(`Can't find element with id: ${after}`);\n }\n\n this.updateStructure(newStruct, (blockId) => {\n if (blockId !== newBlockId) throw new Error(\"Unexpected\");\n this.initializeBlockDuplicate(blockId, originalBlockInfo);\n });\n }\n\n public deleteBlock(blockId: string): void {\n const newStruct = this.structure; // copy current structure\n let done = false;\n for (const group of newStruct.groups) {\n const idx = group.blocks.findIndex((b) => b.id === blockId);\n if (idx < 0) continue;\n group.blocks.splice(idx, 1);\n done = true;\n break;\n }\n if (!done) throw new Error(`Can't find element with id: ${blockId}`);\n this.updateStructure(newStruct);\n }\n\n //\n // Block-pack migration\n //\n\n public migrateBlockPack(\n blockId: string,\n spec: BlockPackSpecPrepared,\n newClearState?: ClearState,\n ): void {\n const info = this.getBlockInfo(blockId);\n const newConfig = extractConfig(spec.config);\n\n const persistBlockPack = () => {\n this.setBlockField(\n blockId,\n \"blockPack\",\n Pl.wrapInHolder(this.tx, createBlockPack(this.tx, spec)),\n \"NotReady\",\n );\n };\n\n const applyStorageAndDeriveArgs = (storageJson: string) => {\n persistBlockPack();\n this.setBlockStorageRaw(blockId, storageJson);\n const deriveArgsResult = this.projectHelper.deriveArgsFromStorage(newConfig, storageJson);\n if (!deriveArgsResult.error) {\n this.setBlockFieldObj(\n blockId,\n \"currentArgs\",\n this.createJsonFieldValue(deriveArgsResult.value),\n );\n const prerunArgs = this.projectHelper.derivePrerunArgsFromStorage(newConfig, storageJson);\n if (prerunArgs !== undefined) {\n this.setBlockFieldObj(\n blockId,\n \"currentPrerunArgs\",\n this.createJsonFieldValue(prerunArgs),\n );\n } else {\n this.deleteBlockFields(blockId, \"currentPrerunArgs\");\n }\n } else {\n this.deleteBlockFields(blockId, \"currentArgs\");\n this.deleteBlockFields(blockId, \"currentPrerunArgs\");\n }\n };\n\n if (newClearState !== undefined) {\n // State is being reset - no migration needed\n const supportsStorageFromVM = newConfig.modelAPIVersion === BLOCK_STORAGE_FACADE_VERSION;\n\n if (supportsStorageFromVM) {\n // V2+: Get initial storage directly from VM and derive args from it\n const initialStorageJson = this.projectHelper.getInitialStorageInVM(newConfig);\n applyStorageAndDeriveArgs(initialStorageJson);\n this.blocksWithChangedInputs.add(blockId);\n this.updateLastModified();\n } else {\n // V1: Use setStates with legacy state format\n persistBlockPack();\n this.setStates([{ modelAPIVersion: 1, blockId, state: newClearState.state }]);\n }\n } else {\n // State is being preserved - run migrations if needed via VM\n // Only Model API v2 blocks support migrations\n const supportsStateMigrations = newConfig.modelAPIVersion === BLOCK_STORAGE_FACADE_VERSION;\n\n if (supportsStateMigrations) {\n const currentStorageJson = info.blockStorageJson;\n\n // Attempt migration BEFORE persisting block pack — on failure,\n // block stays on old version (no inconsistent new-code/old-storage state)\n const migrationResult = this.projectHelper.migrateStorageInVM(\n newConfig,\n currentStorageJson,\n );\n\n if (migrationResult.error !== undefined) {\n throw new Error(\n `[migrateBlockPack] Block ${blockId} migration failed: ${migrationResult.error}`,\n );\n }\n\n this.projectHelper.logger.info(\n `[migrateBlockPack] Block ${blockId}: ${migrationResult.info}`,\n );\n applyStorageAndDeriveArgs(migrationResult.newStorageJson);\n } else {\n // Legacy blocks (modelAPIVersion 1): persist block pack, set prerunArgs = currentArgs\n persistBlockPack();\n if (info.fields.currentArgs !== undefined) {\n this.setBlockFieldObj(blockId, \"currentPrerunArgs\", info.fields.currentArgs);\n }\n }\n\n this.blocksWithChangedInputs.add(blockId);\n\n // resetting staging outputs for all downstream blocks\n this.getStagingGraph().traverse(\"downstream\", [blockId], ({ id }) => this.resetStaging(id));\n }\n\n // also reset or limbo all downstream productions\n if (info.productionRendered)\n this.getActualProductionGraph().traverse(\"downstream\", [blockId], ({ id }) =>\n this.resetOrLimboProduction(id),\n );\n\n this.updateLastModified();\n }\n\n //\n // Render\n //\n\n public renderProduction(blockIds: string[], addUpstreams: boolean = false): Set<string> {\n const blockIdsSet = new Set(blockIds);\n\n const prodGraph = this.getPendingProductionGraph();\n if (addUpstreams)\n // adding all upstreams automatically\n prodGraph.traverse(\"upstream\", blockIds, (node) => {\n blockIdsSet.add(node.id);\n });\n else\n // checking that targets contain all upstreams\n for (const blockId of blockIdsSet) {\n const node = prodGraph.nodes.get(blockId);\n if (node === undefined) throw new Error(`Can't find block with id: ${blockId}`);\n for (const upstream of node.upstream)\n if (!blockIdsSet.has(upstream))\n throw new Error(\"Can't render blocks not including all upstreams.\");\n }\n\n // traversing in topological order and rendering target blocks\n const rendered = new Set<string>();\n for (const block of allBlocks(this.structure)) {\n if (!blockIdsSet.has(block.id)) continue;\n\n let render =\n this.getBlockInfo(block.id).requireProductionRendering || this.blocksInLimbo.has(block.id);\n\n if (!render)\n for (const upstream of prodGraph.nodes.get(block.id)!.upstream)\n if (rendered.has(upstream)) {\n render = true;\n break;\n }\n\n if (render) {\n this.renderProductionFor(block.id);\n rendered.add(block.id);\n }\n }\n\n const renderedArray = [...rendered];\n\n // sending to limbo all downstream blocks\n prodGraph.traverse(\"downstream\", renderedArray, (node) => {\n if (rendered.has(node.id))\n // don't send to limbo blocks that were just rendered\n return;\n this.resetOrLimboProduction(node.id);\n });\n\n // resetting staging outputs for all downstream blocks\n this.getStagingGraph().traverse(\"downstream\", renderedArray, ({ id }) => {\n // don't reset staging of the first rendered block\n if (renderedArray[0] !== id) this.resetStaging(id);\n });\n\n if (rendered.size > 0) this.updateLastModified();\n\n return rendered;\n }\n\n /** Stops running blocks from the list and modify states of other blocks\n * accordingly */\n public stopProduction(...blockIds: string[]) {\n const activeProdGraph = this.getActualProductionGraph();\n\n // we will stop all blocks listed in request and all their downstreams\n const queue = new Denque(blockIds);\n const queued = new Set(blockIds);\n const stopped: string[] = [];\n\n while (!queue.isEmpty()) {\n const blockId = queue.shift()!;\n const fields = this.getBlockInfo(blockId).fields;\n\n if (fields.prodOutput?.status === \"Ready\" && fields.prodCtx?.status === \"Ready\")\n // skipping finished blocks\n continue;\n\n if (this.deleteBlockFields(blockId, \"prodOutput\", \"prodCtx\", \"prodUiCtx\", \"prodArgs\")) {\n // was actually stopped\n stopped.push(blockId);\n\n // will try to stop all its downstreams\n for (const downstream of activeProdGraph.traverseIdsExcludingRoots(\"downstream\", blockId)) {\n if (queued.has(downstream)) continue;\n queue.push(downstream);\n queued.add(downstream);\n }\n }\n }\n\n // blocks under stopped blocks, but having finished production results, goes to limbo\n for (const blockId of activeProdGraph.traverseIdsExcludingRoots(\"downstream\", ...stopped))\n this.resetOrLimboProduction(blockId);\n\n // reset staging outputs for all downstream blocks\n this.getStagingGraph().traverse(\"downstream\", stopped, ({ id }) => this.resetStaging(id));\n }\n\n private traverseWithStagingLag(cb: (blockId: string, lag: number) => void) {\n const lags = new Map<string, number>();\n const stagingGraph = this.getStagingGraph();\n stagingGraph.nodes.forEach((node) => {\n const info = this.getBlockInfo(node.id);\n // Use requireStagingRendering to check both: staging exists AND prerunArgs hasn't changed\n const requiresRendering = info.requireStagingRendering;\n let lag = requiresRendering ? 1 : 0;\n node.upstream.forEach((upstream) => {\n const upstreamLag = lags.get(upstream)!;\n if (upstreamLag === 0) return;\n lag = Math.max(upstreamLag + 1, lag);\n });\n if (!requiresRendering && info.stagingRendered) {\n // console.log(`[traverseWithStagingLag] SKIP staging for ${node.id} - prerunArgs unchanged`);\n }\n cb(node.id, lag);\n lags.set(node.id, lag);\n });\n }\n\n /** @param stagingRenderingRate rate in blocks per second */\n private refreshStagings(stagingRenderingRate?: number) {\n const elapsed = Date.now() - this.renderingState.stagingRefreshTimestamp;\n const lagThreshold =\n stagingRenderingRate === undefined\n ? undefined\n : 1 + Math.max(0, (elapsed * stagingRenderingRate) / 1000);\n let rendered = 0;\n this.traverseWithStagingLag((blockId, lag) => {\n if (lag === 0)\n // meaning staging already rendered\n return;\n if (lagThreshold === undefined || lag <= lagThreshold) {\n try {\n this.renderStagingFor(blockId);\n rendered++;\n } catch (e) {\n this.projectHelper.logger.error(\n new Error(`[refreshStagings] renderStagingFor failed for ${blockId}`, { cause: e }),\n );\n }\n }\n });\n if (rendered > 0) this.resetStagingRefreshTimestamp();\n }\n\n //\n // Meta\n //\n\n /** Updates project metadata */\n public setMeta(meta: ProjectMeta): void {\n this.meta = meta;\n this.metaChanged = true;\n this.updateLastModified();\n }\n\n //\n // Maintenance\n //\n\n /** @param stagingRenderingRate rate in blocks per second */\n public doRefresh(stagingRenderingRate?: number) {\n this.refreshStagings(stagingRenderingRate);\n this.blockInfos.forEach((blockInfo) => {\n if (\n blockInfo.fields.prodCtx?.status === \"Ready\" &&\n blockInfo.fields.prodOutput?.status === \"Ready\"\n )\n this.deleteBlockFields(\n blockInfo.id,\n \"prodOutputPrevious\",\n \"prodCtxPrevious\",\n \"prodUiCtxPrevious\",\n );\n if (\n blockInfo.fields.stagingCtx?.status === \"Ready\" &&\n blockInfo.fields.stagingOutput?.status === \"Ready\"\n )\n this.deleteBlockFields(\n blockInfo.id,\n \"stagingOutputPrevious\",\n \"stagingCtxPrevious\",\n \"stagingUiCtxPrevious\",\n );\n });\n }\n\n private assignAuthorMarkers() {\n const markerStr = this.author ? JSON.stringify(this.author) : undefined;\n\n for (const blockId of this.blocksWithChangedInputs)\n if (markerStr === undefined) this.tx.deleteKValue(this.rid, blockArgsAuthorKey(blockId));\n else this.tx.setKValue(this.rid, blockArgsAuthorKey(blockId), markerStr);\n\n if (this.metaChanged || this.structureChanged) {\n if (markerStr === undefined) this.tx.deleteKValue(this.rid, ProjectStructureAuthorKey);\n else this.tx.setKValue(this.rid, ProjectStructureAuthorKey, markerStr);\n }\n }\n\n public save() {\n if (!this.wasModified) return;\n\n if (this.lastModifiedChanged)\n this.tx.setKValue(this.rid, ProjectLastModifiedTimestamp, JSON.stringify(this.lastModified));\n\n if (this.structureChanged)\n this.tx.setKValue(this.rid, ProjectStructureKey, JSON.stringify(this.struct));\n\n if (this.renderingStateChanged)\n this.tx.setKValue(\n this.rid,\n BlockRenderingStateKey,\n JSON.stringify({\n ...this.renderingState,\n blocksInLimbo: [...this.blocksInLimbo],\n } as ProjectRenderingState),\n );\n\n if (this.metaChanged) this.tx.setKValue(this.rid, ProjectMetaKey, JSON.stringify(this.meta));\n\n this.assignAuthorMarkers();\n }\n\n public static async load(\n projectHelper: ProjectHelper,\n tx: PlTransaction,\n rid: ResourceId,\n author?: AuthorMarker,\n ): Promise<ProjectMutator> {\n //\n // Sending initial requests to read project state (start of round-trip #1)\n //\n\n const fullResourceStateP = tx.getResourceData(rid, true);\n const schemaP = tx.getKValueJson<string>(rid, SchemaVersionKey);\n const lastModifiedP = tx.getKValueJson<number>(rid, ProjectLastModifiedTimestamp);\n const metaP = tx.getKValueJson<ProjectMeta>(rid, ProjectMetaKey);\n const structureP = tx.getKValueJson<ProjectStructure>(rid, ProjectStructureKey);\n const renderingStateP = tx.getKValueJson<ProjectRenderingState>(rid, BlockRenderingStateKey);\n\n const fullResourceState = await fullResourceStateP;\n\n // loading field information\n const blockInfoStates = new Map<string, BlockInfoState>();\n for (const f of fullResourceState.fields) {\n const projectField = parseProjectField(f.name);\n\n // processing only fields with known structure\n if (projectField === undefined) continue;\n\n let info = blockInfoStates.get(projectField.blockId);\n if (info === undefined) {\n info = {\n id: projectField.blockId,\n fields: {},\n };\n blockInfoStates.set(projectField.blockId, info);\n }\n\n info.fields[projectField.fieldName] = isNullResourceId(f.value)\n ? { modCount: 0 }\n : { modCount: 0, ref: f.value };\n }\n\n //\n // Roundtrip #1 not yet finished, but as soon as field list is received,\n // we can start sending requests to read states of referenced resources\n // (start of round-trip #2)\n //\n\n const blockFieldRequests: [\n BlockInfoState,\n ProjectField[\"fieldName\"],\n BlockFieldState,\n Promise<BasicResourceData | ResourceData>,\n ][] = [];\n blockInfoStates.forEach((info) => {\n const fields = info.fields;\n for (const [fName, state] of Object.entries(fields)) {\n if (state.ref === undefined) continue;\n if (!isResource(state.ref) || isResourceRef(state.ref))\n throw new Error(\"unexpected behaviour\");\n const fieldName = fName as ProjectField[\"fieldName\"];\n blockFieldRequests.push([\n info,\n fieldName,\n state,\n tx.getResourceData(state.ref, fieldName == \"blockPack\"),\n ]);\n }\n });\n\n // loading jsons\n const [schema, lastModified, meta, structure, { stagingRefreshTimestamp, blocksInLimbo }] =\n await Promise.all([schemaP, lastModifiedP, metaP, structureP, renderingStateP]);\n\n // Checking schema version of the project\n if (schema !== SchemaVersionCurrent) {\n if (Number(schema) < Number(SchemaVersionCurrent))\n throw new UiError(\n `Can't perform this action on this project because it has older schema. Try (re)loading the project to update it.`,\n );\n else\n throw new UiError(\n `Can't perform this action on this project because it has newer schema. Upgrade your desktop app to the latest version.`,\n );\n }\n\n //\n // <- at this point we have all the responses from round-trip #1\n //\n\n //\n // Receiving responses from round-trip #2 and sending requests to read block pack descriptions\n // (start of round-trip #3)\n //\n\n const blockPackRequests: [BlockInfoState, Promise<BasicResourceData>][] = [];\n for (const [info, fieldName, state, response] of blockFieldRequests) {\n const result = await response;\n state.value = result.data;\n if (isNotNullResourceId(result.error)) state.status = \"Error\";\n else if (result.resourceReady || isNotNullResourceId(result.originalResourceId))\n state.status = \"Ready\";\n else state.status = \"NotReady\";\n\n // For block pack we need to traverse the ref field from the resource data\n if (fieldName === \"blockPack\") {\n const refField = (result as ResourceData).fields.find((f) => f.name === Pl.HolderRefField);\n if (refField === undefined) throw new Error(\"Block pack ref field is missing\");\n blockPackRequests.push([\n info,\n tx.getResourceData(ensureResourceIdNotNull(refField.value), false),\n ]);\n }\n }\n\n //\n // <- at this point we have all the responses from round-trip #2\n //\n\n for (const [info, response] of blockPackRequests) {\n const result = await response;\n const bpInfo = cachedDeserialize<BlockPackInfo>(notEmpty(result.data));\n info.blockConfig = extractConfig(bpInfo.config);\n info.blockPack = bpInfo.source;\n }\n\n //\n // <- at this point we have all the responses from round-trip #3\n //\n\n // loading ctx export template to check if we already have cached materialized template in our project\n const ctxExportTplEnvelope = await getPreparedExportTemplateEnvelope();\n\n // expected field name\n const ctxExportTplCacheFieldName = getServiceTemplateField(ctxExportTplEnvelope.hash);\n const ctxExportTplField = fullResourceState.fields.find(\n (f) => f.name === ctxExportTplCacheFieldName,\n );\n let ctxExportTplHolder: AnyResourceRef;\n if (ctxExportTplField !== undefined)\n ctxExportTplHolder = ensureResourceIdNotNull(ctxExportTplField.value);\n else {\n ctxExportTplHolder = Pl.wrapInHolder(tx, loadTemplate(tx, ctxExportTplEnvelope.spec));\n tx.createField(\n field(rid, getServiceTemplateField(ctxExportTplEnvelope.hash)),\n \"Dynamic\",\n ctxExportTplHolder,\n );\n }\n\n const renderingState = { stagingRefreshTimestamp };\n const blocksInLimboSet = new Set(blocksInLimbo);\n\n const blockInfos = new Map<string, BlockInfo>();\n blockInfoStates.forEach(({ id, fields, blockConfig, blockPack }) =>\n blockInfos.set(\n id,\n new BlockInfo(id, fields, notEmpty(blockConfig), notEmpty(blockPack), projectHelper.logger),\n ),\n );\n\n // check consistency of project state\n const blockInStruct = new Set<string>();\n for (const b of allBlocks(structure)) {\n if (!blockInfos.has(b.id))\n throw new Error(`Inconsistent project structure: no inputs for ${b.id}`);\n blockInStruct.add(b.id);\n }\n blockInfos.forEach((info) => {\n if (!blockInStruct.has(info.id))\n throw new Error(`Inconsistent project structure: no structure entry for ${info.id}`);\n });\n\n const prj = new ProjectMutator(\n rid,\n tx,\n author,\n schema,\n lastModified,\n meta,\n structure,\n renderingState,\n blocksInLimboSet,\n blockInfos,\n ctxExportTplHolder,\n projectHelper,\n );\n\n prj.fixProblemsAndMigrate();\n\n return prj;\n }\n}\n\nexport interface ProjectState {\n schema: string;\n structure: ProjectStructure;\n renderingState: Omit<ProjectRenderingState, \"blocksInLimbo\">;\n blocksInLimbo: Set<string>;\n blockInfos: Map<string, BlockInfo>;\n}\n\nexport async function createProject(\n tx: PlTransaction,\n meta: ProjectMeta = InitialBlockMeta,\n): Promise<AnyResourceRef> {\n const prj = tx.createEphemeral(ProjectResourceType);\n tx.lock(prj);\n const ts = String(Date.now());\n tx.setKValue(prj, SchemaVersionKey, JSON.stringify(SchemaVersionCurrent));\n tx.setKValue(prj, ProjectCreatedTimestamp, ts);\n tx.setKValue(prj, ProjectLastModifiedTimestamp, ts);\n tx.setKValue(prj, ProjectMetaKey, JSON.stringify(meta));\n tx.setKValue(prj, ProjectStructureKey, JSON.stringify(InitialBlockStructure));\n tx.setKValue(prj, BlockRenderingStateKey, JSON.stringify(InitialProjectRenderingState));\n const ctxExportTplEnvelope = await getPreparedExportTemplateEnvelope();\n tx.createField(\n field(prj, getServiceTemplateField(ctxExportTplEnvelope.hash)),\n \"Dynamic\",\n Pl.wrapInHolder(tx, loadTemplate(tx, ctxExportTplEnvelope.spec)),\n );\n return prj;\n}\n\nexport async function withProject<T>(\n projectHelper: ProjectHelper,\n txOrPl: PlTransaction | PlClient,\n rid: ResourceId,\n cb: (p: ProjectMutator) => T | Promise<T>,\n ops?: Partial<TxOps>,\n): Promise<T> {\n return withProjectAuthored(projectHelper, txOrPl, rid, undefined, cb, ops);\n}\n\nexport async function withProjectAuthored<T>(\n projectHelper: ProjectHelper,\n txOrPl: PlTransaction | PlClient,\n rid: ResourceId,\n author: AuthorMarker | undefined,\n cb: (p: ProjectMutator) => T | Promise<T>,\n ops: Partial<TxOps> = {},\n): Promise<T> {\n if (txOrPl instanceof PlClient) {\n return await txOrPl.withWriteTx(\n \"ProjectAction\" + (ops.name ? `: ${ops.name}` : \"\"),\n async (tx) => {\n const mut = await ProjectMutator.load(projectHelper, tx, rid, author);\n const result = await cb(mut);\n if (!mut.wasModified)\n // skipping save and commit altogether if no modifications were actually made\n return result;\n mut.save();\n await tx.commit();\n if (getDebugFlags().logProjectMutationStat) console.log(JSON.stringify(tx.stat));\n return result;\n },\n ops,\n );\n } else {\n const mut = await ProjectMutator.load(projectHelper, txOrPl, rid, author);\n const result = await cb(mut);\n mut.save();\n return result;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;AAiGA,SAAS,OAAiB,SAAsB,SAA2B;CACzE,IAAI,cAAc;CAClB,IAAI,YAA+B;CACnC,IAAI,QAAuB;AAC3B,cAAa;AACX,MAAI,CAAC,aAAa;AAChB,iBAAc;AACd,eAAY,SAAS;AACrB,WAAQ,SAAS;AACjB,UAAO;;EAET,MAAM,eAAe,SAAS;AAC9B,MAAI,cAAc,cAAc;AAC9B,eAAY;AACZ,WAAQ,SAAS;;AAEnB,SAAO,SAAS;;;AAIpB,IAAM,YAAN,MAAgB;CACd,YACE,AAAgB,IAChB,AAAgB,QAChB,AAAgB,QAChB,AAAgB,QAChB,AAAiB,SAAmB,IAAI,sBAAsB,EAC9D;EALgB;EACA;EACA;EACA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AC,EAC9D,WACA,GAAG,aAAa,IAAI,aAAa,IAAI,qBAAqB,KAAK,CAAC,CACjE;AACD,QAAO;;AAGT,eAAsB,YACpB,eACA,QACA,KACA,IACA,KACY;AACZ,QAAO,oBAAoB,eAAe,QAAQ,KAAK,QAAW,IAAI,IAAI;;AAG5E,eAAsB,oBACpB,eACA,QACA,KACA,QACA,IACA,MAAsB,EAAE,EACZ;AACZ,KAAI,kBAAkB,SACpB,QAAO,MAAM,OAAO,YAClB,mBAAmB,IAAI,OAAO,KAAK,IAAI,SAAS,KAChD,OAAO,OAAO;EACZ,MAAM,MAAM,MAAM,eAAe,KAAK,eAAe,IAAI,KAAK,OAAO;EACrE,MAAM,SAAS,MAAM,GAAG,IAAI;AAC5B,MAAI,CAAC,IAAI,YAEP,QAAO;AACT,MAAI,MAAM;AACV,QAAM,GAAG,QAAQ;AACjB,MAAI,eAAe,CAAC,uBAAwB,SAAQ,IAAI,KAAK,UAAU,GAAG,KAAK,CAAC;AAChF,SAAO;IAET,IACD;MACI;EACL,MAAM,MAAM,MAAM,eAAe,KAAK,eAAe,QAAQ,KAAK,OAAO;EACzE,MAAM,SAAS,MAAM,GAAG,IAAI;AAC5B,MAAI,MAAM;AACV,SAAO"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@milaboratories/pl-middle-layer",
|
|
3
|
-
"version": "1.48.
|
|
3
|
+
"version": "1.48.18",
|
|
4
4
|
"description": "Pl Middle Layer",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"license": "UNLICENSED",
|
|
@@ -30,20 +30,20 @@
|
|
|
30
30
|
"yaml": "^2.8.0",
|
|
31
31
|
"zod": "~3.23.8",
|
|
32
32
|
"@milaboratories/computable": "2.8.6",
|
|
33
|
-
"@milaboratories/pl-client": "2.17.9",
|
|
34
33
|
"@milaboratories/pl-deployments": "2.15.21",
|
|
35
|
-
"@milaboratories/pf-driver": "1.0.63",
|
|
36
34
|
"@milaboratories/pl-drivers": "1.11.61",
|
|
35
|
+
"@milaboratories/pf-driver": "1.0.63",
|
|
36
|
+
"@milaboratories/pl-client": "2.17.9",
|
|
37
37
|
"@milaboratories/pl-errors": "1.1.71",
|
|
38
|
+
"@milaboratories/pl-model-common": "1.25.2",
|
|
38
39
|
"@milaboratories/pl-model-backend": "1.1.56",
|
|
39
40
|
"@milaboratories/pl-http": "1.2.4",
|
|
40
|
-
"@milaboratories/pl-model-common": "1.25.2",
|
|
41
|
-
"@milaboratories/pl-model-middle-layer": "1.12.10",
|
|
42
|
-
"@milaboratories/pl-tree": "1.8.49",
|
|
43
41
|
"@milaboratories/resolve-helper": "1.1.3",
|
|
44
|
-
"@
|
|
42
|
+
"@milaboratories/pl-model-middle-layer": "1.12.10",
|
|
45
43
|
"@milaboratories/ts-helpers": "1.7.3",
|
|
46
44
|
"@platforma-sdk/block-tools": "2.6.65",
|
|
45
|
+
"@milaboratories/pl-tree": "1.8.49",
|
|
46
|
+
"@platforma-sdk/model": "1.58.11",
|
|
47
47
|
"@platforma-sdk/workflow-tengo": "5.9.1"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
@@ -53,9 +53,9 @@
|
|
|
53
53
|
"semver": "^7.7.2",
|
|
54
54
|
"typescript": "~5.9.3",
|
|
55
55
|
"vitest": "^4.0.18",
|
|
56
|
-
"@milaboratories/build-configs": "1.5.2",
|
|
57
56
|
"@milaboratories/ts-configs": "1.2.2",
|
|
58
|
-
"@milaboratories/
|
|
57
|
+
"@milaboratories/build-configs": "1.5.2",
|
|
58
|
+
"@milaboratories/ts-builder": "1.3.0"
|
|
59
59
|
},
|
|
60
60
|
"engines": {
|
|
61
61
|
"node": ">=22.19.0"
|
package/src/mutator/project.ts
CHANGED
|
@@ -327,14 +327,44 @@ export class ProjectMutator {
|
|
|
327
327
|
) {}
|
|
328
328
|
|
|
329
329
|
private fixProblemsAndMigrate() {
|
|
330
|
-
//
|
|
330
|
+
// Fix inconsistent production fields
|
|
331
331
|
this.blockInfos.forEach((blockInfo) => {
|
|
332
332
|
if (
|
|
333
333
|
blockInfo.fields.prodArgs === undefined ||
|
|
334
334
|
blockInfo.fields.prodOutput === undefined ||
|
|
335
335
|
blockInfo.fields.prodCtx === undefined
|
|
336
336
|
)
|
|
337
|
-
this.deleteBlockFields(blockInfo.id, "prodArgs", "prodOutput", "prodCtx");
|
|
337
|
+
this.deleteBlockFields(blockInfo.id, "prodArgs", "prodOutput", "prodCtx", "prodUiCtx");
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
// Fix inconsistent staging fields
|
|
341
|
+
this.blockInfos.forEach((blockInfo) => {
|
|
342
|
+
if (blockInfo.fields.stagingOutput === undefined || blockInfo.fields.stagingCtx === undefined)
|
|
343
|
+
this.deleteBlockFields(blockInfo.id, "stagingOutput", "stagingCtx", "stagingUiCtx");
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
// Fix inconsistent cache fields
|
|
347
|
+
this.blockInfos.forEach((blockInfo) => {
|
|
348
|
+
if (
|
|
349
|
+
blockInfo.fields.prodOutputPrevious === undefined ||
|
|
350
|
+
blockInfo.fields.prodCtxPrevious === undefined
|
|
351
|
+
)
|
|
352
|
+
this.deleteBlockFields(
|
|
353
|
+
blockInfo.id,
|
|
354
|
+
"prodOutputPrevious",
|
|
355
|
+
"prodCtxPrevious",
|
|
356
|
+
"prodUiCtxPrevious",
|
|
357
|
+
);
|
|
358
|
+
if (
|
|
359
|
+
blockInfo.fields.stagingOutputPrevious === undefined ||
|
|
360
|
+
blockInfo.fields.stagingCtxPrevious === undefined
|
|
361
|
+
)
|
|
362
|
+
this.deleteBlockFields(
|
|
363
|
+
blockInfo.id,
|
|
364
|
+
"stagingOutputPrevious",
|
|
365
|
+
"stagingCtxPrevious",
|
|
366
|
+
"stagingUiCtxPrevious",
|
|
367
|
+
);
|
|
338
368
|
});
|
|
339
369
|
|
|
340
370
|
// Migration for addition of block settings field
|
|
@@ -346,6 +376,9 @@ export class ProjectMutator {
|
|
|
346
376
|
this.setBlockFieldObj(blockInfo.id, "blockSettings", initialBlockSettings);
|
|
347
377
|
}
|
|
348
378
|
});
|
|
379
|
+
|
|
380
|
+
// Validate after fixes
|
|
381
|
+
this.blockInfos.forEach((info) => info.check());
|
|
349
382
|
}
|
|
350
383
|
|
|
351
384
|
get wasModified(): boolean {
|
|
@@ -812,9 +845,14 @@ export class ProjectMutator {
|
|
|
812
845
|
const upstreamContexts: AnyRef[] = [];
|
|
813
846
|
upstream.forEach((id) => {
|
|
814
847
|
const info = this.getBlockInfo(id);
|
|
815
|
-
if (info.fields["stagingCtx"]?.ref
|
|
816
|
-
|
|
817
|
-
|
|
848
|
+
if (info.fields["stagingCtx"]?.ref !== undefined) {
|
|
849
|
+
upstreamContexts.push(Pl.unwrapHolder(this.tx, info.fields["stagingCtx"].ref));
|
|
850
|
+
} else if (info.fields.currentPrerunArgs !== undefined) {
|
|
851
|
+
// Upstream has currentPrerunArgs but no staging — this is an inconsistency
|
|
852
|
+
throw new Error(`Upstream ${id} staging is not rendered but has currentPrerunArgs set.`);
|
|
853
|
+
}
|
|
854
|
+
// Blocks without currentPrerunArgs (e.g. block model doesn't define prerunArgs, or args
|
|
855
|
+
// derivation failed) never get stagingCtx. Use prodCtx if available.
|
|
818
856
|
if (info.fields["prodCtx"]?.ref !== undefined)
|
|
819
857
|
upstreamContexts.push(Pl.unwrapHolder(this.tx, info.fields["prodCtx"].ref));
|
|
820
858
|
});
|
|
@@ -830,16 +868,19 @@ export class ProjectMutator {
|
|
|
830
868
|
* If currentPrerunArgs is not set (prerunArgs returned undefined), skips staging for this block.
|
|
831
869
|
*/
|
|
832
870
|
private renderStagingFor(blockId: string) {
|
|
833
|
-
this.resetStaging(blockId);
|
|
834
|
-
|
|
835
871
|
const info = this.getBlockInfo(blockId);
|
|
836
872
|
|
|
837
|
-
//
|
|
873
|
+
// Check BEFORE resetStaging: if currentPrerunArgs is not set (e.g. prerunArgs() returned undefined
|
|
874
|
+
// because inputs aren't ready, or args derivation failed), skip without clearing existing staging.
|
|
875
|
+
// Otherwise resetStaging would delete stagingCtx, and downstream blocks that reference this block
|
|
876
|
+
// as an upstream would fail in createStagingCtx.
|
|
838
877
|
const prerunArgsRef = info.fields.currentPrerunArgs?.ref;
|
|
839
878
|
if (prerunArgsRef === undefined) {
|
|
840
879
|
return;
|
|
841
880
|
}
|
|
842
881
|
|
|
882
|
+
this.resetStaging(blockId);
|
|
883
|
+
|
|
843
884
|
const ctx = this.createStagingCtx(this.getStagingGraph().nodes.get(blockId)!.upstream);
|
|
844
885
|
|
|
845
886
|
if (this.getBlock(blockId).renderingMode !== "Heavy") throw new Error("not supported yet");
|
|
@@ -1700,8 +1741,6 @@ export class ProjectMutator {
|
|
|
1700
1741
|
blockInfos.forEach((info) => {
|
|
1701
1742
|
if (!blockInStruct.has(info.id))
|
|
1702
1743
|
throw new Error(`Inconsistent project structure: no structure entry for ${info.id}`);
|
|
1703
|
-
// checking structure
|
|
1704
|
-
info.check();
|
|
1705
1744
|
});
|
|
1706
1745
|
|
|
1707
1746
|
const prj = new ProjectMutator(
|