@milaboratories/pl-middle-layer 1.54.3 → 1.54.4
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
|
@@ -31,7 +31,7 @@ function cached(modIdCb, valueCb) {
|
|
|
31
31
|
lastModId = currentModId;
|
|
32
32
|
value = valueCb();
|
|
33
33
|
}
|
|
34
|
-
return
|
|
34
|
+
return value;
|
|
35
35
|
};
|
|
36
36
|
}
|
|
37
37
|
var BlockInfo = class {
|
|
@@ -44,9 +44,13 @@ var BlockInfo = class {
|
|
|
44
44
|
}
|
|
45
45
|
check() {
|
|
46
46
|
if (this.fields.prodOutput === void 0 !== (this.fields.prodCtx === void 0)) throw new Error("inconsistent prod fields");
|
|
47
|
+
if (this.fields.prodOutput === void 0 !== (this.fields.prodUiCtx === void 0)) throw new Error("inconsistent prod fields (prodUiCtx)");
|
|
47
48
|
if (this.fields.stagingOutput === void 0 !== (this.fields.stagingCtx === void 0)) throw new Error("inconsistent stage fields");
|
|
49
|
+
if (this.fields.stagingOutput === void 0 !== (this.fields.stagingUiCtx === void 0)) throw new Error("inconsistent stage fields (stagingUiCtx)");
|
|
48
50
|
if (this.fields.prodOutputPrevious === void 0 !== (this.fields.prodCtxPrevious === void 0)) throw new Error("inconsistent prod cache fields");
|
|
51
|
+
if (this.fields.prodOutputPrevious === void 0 !== (this.fields.prodUiCtxPrevious === void 0)) throw new Error("inconsistent prod cache fields (prodUiCtxPrevious)");
|
|
49
52
|
if (this.fields.stagingOutputPrevious === void 0 !== (this.fields.stagingCtxPrevious === void 0)) throw new Error("inconsistent stage cache fields");
|
|
53
|
+
if (this.fields.stagingOutputPrevious === void 0 !== (this.fields.stagingUiCtxPrevious === void 0)) throw new Error("inconsistent stage cache fields (stagingUiCtxPrevious)");
|
|
50
54
|
if (this.fields.blockPack === void 0) throw new Error("no block pack field");
|
|
51
55
|
if (this.fields.blockStorage === void 0) throw new Error("no block storage field");
|
|
52
56
|
}
|
|
@@ -147,14 +151,14 @@ var ProjectMutator = class ProjectMutator {
|
|
|
147
151
|
}
|
|
148
152
|
fixProblemsAndMigrate() {
|
|
149
153
|
this.blockInfos.forEach((blockInfo) => {
|
|
150
|
-
if (blockInfo.fields.prodArgs === void 0 || blockInfo.fields.prodOutput === void 0 || blockInfo.fields.prodCtx === void 0) this.deleteBlockFields(blockInfo.id, "prodArgs", "prodOutput", "prodCtx", "prodUiCtx");
|
|
154
|
+
if (blockInfo.fields.prodArgs === void 0 || blockInfo.fields.prodOutput === void 0 || blockInfo.fields.prodCtx === void 0 || blockInfo.fields.prodUiCtx === void 0) this.deleteBlockFields(blockInfo.id, "prodArgs", "prodOutput", "prodCtx", "prodUiCtx");
|
|
151
155
|
});
|
|
152
156
|
this.blockInfos.forEach((blockInfo) => {
|
|
153
|
-
if (blockInfo.fields.stagingOutput === void 0 || blockInfo.fields.stagingCtx === void 0) this.deleteBlockFields(blockInfo.id, "stagingOutput", "stagingCtx", "stagingUiCtx");
|
|
157
|
+
if (blockInfo.fields.stagingOutput === void 0 || blockInfo.fields.stagingCtx === void 0 || blockInfo.fields.stagingUiCtx === void 0) this.deleteBlockFields(blockInfo.id, "stagingOutput", "stagingCtx", "stagingUiCtx");
|
|
154
158
|
});
|
|
155
159
|
this.blockInfos.forEach((blockInfo) => {
|
|
156
|
-
if (blockInfo.fields.prodOutputPrevious === void 0 || blockInfo.fields.prodCtxPrevious === void 0) this.deleteBlockFields(blockInfo.id, "prodOutputPrevious", "prodCtxPrevious", "prodUiCtxPrevious");
|
|
157
|
-
if (blockInfo.fields.stagingOutputPrevious === void 0 || blockInfo.fields.stagingCtxPrevious === void 0) this.deleteBlockFields(blockInfo.id, "stagingOutputPrevious", "stagingCtxPrevious", "stagingUiCtxPrevious");
|
|
160
|
+
if (blockInfo.fields.prodOutputPrevious === void 0 || blockInfo.fields.prodCtxPrevious === void 0 || blockInfo.fields.prodUiCtxPrevious === void 0) this.deleteBlockFields(blockInfo.id, "prodOutputPrevious", "prodCtxPrevious", "prodUiCtxPrevious");
|
|
161
|
+
if (blockInfo.fields.stagingOutputPrevious === void 0 || blockInfo.fields.stagingCtxPrevious === void 0 || blockInfo.fields.stagingUiCtxPrevious === void 0) this.deleteBlockFields(blockInfo.id, "stagingOutputPrevious", "stagingCtxPrevious", "stagingUiCtxPrevious");
|
|
158
162
|
});
|
|
159
163
|
let initialBlockSettings;
|
|
160
164
|
this.blockInfos.forEach((blockInfo) => {
|
|
@@ -1032,7 +1036,12 @@ async function duplicateProject(tx, sourceRid, options) {
|
|
|
1032
1036
|
}
|
|
1033
1037
|
tx.setKValue(newPrj, require_project_model.ProjectCreatedTimestamp, ts);
|
|
1034
1038
|
tx.setKValue(newPrj, require_project_model.ProjectLastModifiedTimestamp, ts);
|
|
1035
|
-
for (const f of sourceData.fields)
|
|
1039
|
+
for (const f of sourceData.fields) {
|
|
1040
|
+
if ((0, _milaboratories_pl_client.isNullResourceId)(f.value)) continue;
|
|
1041
|
+
const parsed = require_project_model.parseProjectField(f.name);
|
|
1042
|
+
if (parsed !== void 0 && !require_project_model.FieldsToDuplicate.has(parsed.fieldName)) continue;
|
|
1043
|
+
tx.createField((0, _milaboratories_pl_client.field)(newPrj, f.name), "Dynamic", f.value);
|
|
1044
|
+
}
|
|
1036
1045
|
return newPrj;
|
|
1037
1046
|
}
|
|
1038
1047
|
async function withProject(projectHelper, txOrPl, rid, cb, ops) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"project.cjs","names":["ConsoleLoggerAdapter","Pl","BlockPackTemplateField","InitialBlockSettings","stagingGraph","productionGraph","allBlocks","projectFieldName","BLOCK_STORAGE_FACADE_VERSION","createBContextFromUpstreams","createBContextEnd","exportContext","createRenderHeavyBlock","createBlockPack","FieldsToDuplicate","graphDiff","Denque","blockArgsAuthorKey","ProjectStructureAuthorKey","ProjectLastModifiedTimestamp","ProjectStructureKey","BlockRenderingStateKey","ProjectMetaKey","SchemaVersionKey","parseProjectField","SchemaVersionCurrent","UiError","getPreparedExportTemplateEnvelope","getServiceTemplateField","loadTemplate","InitialBlockMeta","ProjectResourceType","ProjectCreatedTimestamp","InitialBlockStructure","InitialProjectRenderingState","SchemaVersionV3","BlockArgsAuthorKeyPrefix","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 {\n createRenderHeavyBlock,\n createBContextFromUpstreams,\n createBContextEnd,\n} 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 SchemaVersionV3,\n ProjectResourceType,\n InitialBlockStructure,\n InitialProjectRenderingState,\n ProjectMetaKey,\n InitialBlockMeta,\n blockArgsAuthorKey,\n BlockArgsAuthorKeyPrefix,\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 { gzipSync } from \"node:zlib\";\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 /** Max number of blocks to render staging for in a single background refresh pass. */\n private static readonly STAGING_REFRESH_MAX_BATCH = 10;\n\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 // Migration: build production context chain if not present,\n // and reset all stagings so they re-render using the new chain\n const needsChainBuild = [...this.blockInfos.values()].some(\n (info) => info.fields.prodChainCtx === undefined,\n );\n if (needsChainBuild && this.blockInfos.size > 0) {\n this.rebuildProdChain(0);\n this.blockInfos.forEach((blockInfo) => {\n this.resetStaging(blockInfo.id);\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 /** Create a plain JSON resource value from a JS object (no compression). */\n private createJsonFieldValue(obj: unknown): BlockFieldStateValue {\n const value = Buffer.from(JSON.stringify(obj));\n const ref = this.tx.createValue(Pl.JsonObject, value);\n return { ref, value, status: \"Ready\" };\n }\n\n /** Create a plain JSON resource value from a pre-serialized JSON string (no compression). */\n private createJsonFieldValueFromContent(json: string): BlockFieldStateValue {\n const value = Buffer.from(json);\n const ref = this.tx.createValue(Pl.JsonObject, value);\n return { ref, value, status: \"Ready\" };\n }\n\n /** Create a gzip-compressed JSON resource value from a JS object (compressed if >= 16KB). */\n private createGzJsonFieldValue(obj: unknown): BlockFieldStateValue {\n const jsonBytes = canonicalJsonBytes(obj);\n return this.createGzJsonFieldValueFromBytes(jsonBytes);\n }\n\n /** Create a gzip-compressed JSON resource value from a pre-serialized JSON string (compressed if >= 16KB). */\n private createGzJsonFieldValueFromContent(json: string): BlockFieldStateValue {\n return this.createGzJsonFieldValueFromBytes(Buffer.from(json));\n }\n\n private createGzJsonFieldValueFromBytes(jsonBytes: Uint8Array): BlockFieldStateValue {\n const gzipThreshold = 16_384;\n if (jsonBytes.length >= gzipThreshold) {\n const data = gzipSync(jsonBytes);\n const ref = this.tx.createValue(Pl.JsonGzObject, data);\n return { ref, value: data, status: \"Ready\" };\n }\n const ref = this.tx.createValue(Pl.JsonObject, jsonBytes);\n return { ref, value: jsonBytes, status: \"Ready\" };\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.createGzJsonFieldValueFromContent(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.createGzJsonFieldValueFromContent(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.createGzJsonFieldValue(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 // NB: currentArgs must NOT be gzipped — they are read by Tengo workflows which can't decompress\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 // NB: currentPrerunArgs must NOT be gzipped — they are read by Tengo workflows which can't decompress\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 // Render staging inline for blocks with changed prerunArgs — no downstream cascade.\n // Each block's staging uses only the pre-built production context chain (prodChainCtx),\n // which doesn't change on arg edits, so downstream blocks are unaffected.\n for (const blockId of changedArgs) {\n try {\n this.renderStagingFor(blockId);\n } catch (e) {\n this.projectHelper.logger.error(\n new Error(`[setStates] inline staging render failed for ${blockId}`, { cause: e }),\n );\n }\n }\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 /**\n * Rebuilds the production context chain from `fromBlockIndex` to the end.\n * Each block gets a `prodChainCtx` field containing the accumulated production\n * contexts from all blocks above it in project order.\n *\n * Construction rule for block at position K:\n * - If block K-1 has prodCtx: chainNode[K] = BContext(chainNode[K-1], K-1.prodCtx)\n * - If block K-1 has no prodCtx: chainNode[K] = chainNode[K-1] (passthrough)\n * - Block 0: chainNode[0] = BContextEnd\n */\n private rebuildProdChain(fromBlockIndex: number = 0): void {\n const blocks = [...allBlocks(this.struct)];\n if (fromBlockIndex >= blocks.length) return;\n\n // Get the inner chain context ref of the block before fromBlockIndex\n let prevChainCtx: AnyRef | undefined;\n if (fromBlockIndex > 0) {\n const prevHolder = this.getBlockInfo(blocks[fromBlockIndex - 1].id).fields.prodChainCtx?.ref;\n if (prevHolder === undefined) {\n throw new Error(\n `rebuildProdChain(${fromBlockIndex}): block ${blocks[fromBlockIndex - 1].id} at position ${fromBlockIndex - 1} has no prodChainCtx — chain must be built from 0 first`,\n );\n }\n prevChainCtx = Pl.unwrapHolder(this.tx, prevHolder);\n }\n\n for (let i = fromBlockIndex; i < blocks.length; i++) {\n const blockId = blocks[i].id;\n\n let newChainCtx: AnyRef;\n\n if (i === 0) {\n // First block: nothing above\n newChainCtx = createBContextEnd(this.tx);\n } else {\n const prevBlockId = blocks[i - 1].id;\n const prevInfo = this.getBlockInfo(prevBlockId);\n const prevProdCtxHolder = prevInfo.fields.prodCtx?.ref;\n\n if (prevProdCtxHolder !== undefined) {\n // Block above has production: accumulate into chain\n const upstreams: AnyRef[] = [];\n upstreams.push(prevChainCtx!);\n upstreams.push(Pl.unwrapHolder(this.tx, prevProdCtxHolder));\n newChainCtx = createBContextFromUpstreams(this.tx, upstreams);\n } else {\n // Passthrough: reuse inner context from previous block\n newChainCtx = prevChainCtx!;\n }\n }\n\n // Store chain node (wrapped in holder)\n this.setBlockField(\n blockId,\n \"prodChainCtx\",\n Pl.wrapInEphHolder(this.tx, newChainCtx),\n \"NotReady\",\n );\n\n prevChainCtx = newChainCtx;\n }\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 // Skip if currentPrerunArgs is not set (prerunArgs() returned undefined or args derivation failed)\n const prerunArgsRef = info.fields.currentPrerunArgs?.ref;\n if (prerunArgsRef === undefined) {\n return;\n }\n\n this.resetStaging(blockId);\n\n // Use the pre-built production context chain node\n const chainCtxHolder = info.fields.prodChainCtx?.ref;\n if (chainCtxHolder === undefined) {\n throw new Error(\n `[renderStagingFor] block ${blockId} has no prodChainCtx — chain must be built before staging render`,\n );\n }\n const ctx = Pl.unwrapHolder(this.tx, chainCtxHolder);\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.createGzJsonFieldValueFromContent(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 // Rebuild production chain — structure (and thus block order) may have changed.\n // Find the first position that changed to avoid rebuilding the entire chain.\n const newBlocks = [...allBlocks(newStructure)];\n const oldBlocks = [...currentStagingGraph.nodes.keys()];\n const n = Math.min(newBlocks.length, oldBlocks.length);\n let firstChangedIdx = 0;\n while (firstChangedIdx < n && newBlocks[firstChangedIdx].id === oldBlocks[firstChangedIdx]) {\n firstChangedIdx++;\n }\n if (firstChangedIdx < newBlocks.length) {\n this.rebuildProdChain(firstChangedIdx);\n }\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\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 // Rebuild chain from this block onward (production may have been reset/limboed)\n // and reset staging for the migrated block + everything below\n const blocksList = [...allBlocks(this.struct)];\n const blockIdx = blocksList.findIndex((b) => b.id === blockId);\n if (blockIdx >= 0) {\n this.rebuildProdChain(blockIdx + 1);\n for (let i = blockIdx; i < blocksList.length; i++) {\n this.resetStaging(blocksList[i].id);\n }\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 // Rebuild production chain and reset downstream staging\n if (rendered.size > 0) {\n const blocksList = [...allBlocks(this.struct)];\n const firstRenderedIdx = blocksList.findIndex((b) => rendered.has(b.id));\n if (firstRenderedIdx >= 0) {\n // Chain from firstRenderedIdx+1 onward changes (rendered blocks have new prodCtx)\n this.rebuildProdChain(firstRenderedIdx + 1);\n // Reset staging for all blocks after the first rendered block\n // (their chain node changed; the first rendered block's chain is unaffected)\n for (let i = firstRenderedIdx + 1; i < blocksList.length; i++) {\n this.resetStaging(blocksList[i].id);\n }\n }\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 // Rebuild chain and reset staging for stopped blocks and everything below\n if (stopped.length > 0) {\n const blocksList = [...allBlocks(this.struct)];\n const stoppedSet = new Set(stopped);\n const firstStoppedIdx = blocksList.findIndex((b) => stoppedSet.has(b.id));\n if (firstStoppedIdx >= 0) {\n // Stopped blocks lost prodCtx — chain below them changes\n this.rebuildProdChain(firstStoppedIdx + 1);\n for (let i = firstStoppedIdx; i < blocksList.length; i++) {\n this.resetStaging(blocksList[i].id);\n }\n }\n }\n }\n\n /** Renders staging for blocks that need it (have currentPrerunArgs but no staging). */\n private refreshStagings() {\n const maxBatch = ProjectMutator.STAGING_REFRESH_MAX_BATCH;\n let rendered = 0;\n for (const block of allBlocks(this.struct)) {\n if (rendered >= maxBatch) break;\n const info = this.getBlockInfo(block.id);\n if (info.requireStagingRendering) {\n try {\n this.renderStagingFor(block.id);\n rendered++;\n } catch (e) {\n this.projectHelper.logger.error(\n new Error(`[refreshStagings] renderStagingFor failed for ${block.id}`, { 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 /** Background maintenance: render pending stagings + GC of Previous fields. */\n public doRefresh() {\n this.refreshStagings();\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\n/**\n * Duplicates an existing project resource within a transaction.\n * Creates a new project resource with all dynamic fields (block data) copied by reference\n * and all KV metadata copied with appropriate adjustments.\n *\n * The returned resource is NOT attached to any project list — the caller is responsible\n * for placing it where needed. This enables cross-user duplication.\n *\n * @param tx - active write transaction\n * @param sourceRid - resource id of the project to duplicate\n * @param options.label - optional label override for the new project; if omitted, copies the source label\n */\nexport async function duplicateProject(\n tx: PlTransaction,\n sourceRid: ResourceId,\n options?: { label?: string },\n): Promise<AnyResourceRef> {\n // Read source resource data (with fields) and all KV pairs\n const sourceDataP = tx.getResourceData(sourceRid, true);\n const sourceKVsP = tx.listKeyValuesString(sourceRid);\n\n const sourceData = await sourceDataP;\n const sourceKVs = await sourceKVsP;\n\n // Validate schema version (accept current and previous version that can be migrated on open)\n const schemaKV = sourceKVs.find((kv) => kv.key === SchemaVersionKey);\n const schema = schemaKV ? JSON.parse(schemaKV.value) : undefined;\n if (schema !== SchemaVersionCurrent && schema !== SchemaVersionV3) {\n throw new UiError(\n `Cannot duplicate project with schema version ${schema ?? \"unknown\"}. ` +\n `Only schema versions ${SchemaVersionV3} and ${SchemaVersionCurrent} are supported. ` +\n `Try opening the project first to trigger migration.`,\n );\n }\n\n // Create new project resource\n const newPrj = tx.createEphemeral(ProjectResourceType);\n tx.lock(newPrj);\n\n // Copy KV pairs with adjustments\n const ts = String(Date.now());\n const kvSkipPrefixes = [BlockArgsAuthorKeyPrefix];\n const kvSkipKeys = new Set([\n ProjectCreatedTimestamp,\n ProjectLastModifiedTimestamp,\n ProjectStructureAuthorKey,\n ]);\n\n for (const { key, value } of sourceKVs) {\n // Skip author markers\n if (kvSkipKeys.has(key)) continue;\n if (kvSkipPrefixes.some((prefix) => key.startsWith(prefix))) continue;\n\n if (key === ProjectMetaKey && options?.label !== undefined) {\n // Override label\n const meta: ProjectMeta = JSON.parse(value);\n tx.setKValue(newPrj, key, JSON.stringify({ ...meta, label: options.label }));\n } else {\n // Copy as-is\n tx.setKValue(newPrj, key, value);\n }\n }\n\n // Set fresh timestamps\n tx.setKValue(newPrj, ProjectCreatedTimestamp, ts);\n tx.setKValue(newPrj, ProjectLastModifiedTimestamp, ts);\n\n // Copy all dynamic fields by sharing references\n for (const f of sourceData.fields) {\n if (isNotNullResourceId(f.value)) {\n tx.createField(field(newPrj, f.name), \"Dynamic\", f.value);\n }\n }\n\n return newPrj;\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":";;;;;;;;;;;;;;;;;AAwGA,SAAS,OAAiB,SAAsB,SAA2B;CACzE,IAAI,cAAc;CAClB,IAAI,YAA+B;CACnC,IAAI,QAAuB;AAC3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,KAAK,OAAO;EACrE,MAAM,SAAS,MAAM,GAAG,IAAI;AAC5B,MAAI,CAAC,IAAI,YAEP,QAAO;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"}
|
|
1
|
+
{"version":3,"file":"project.cjs","names":["ConsoleLoggerAdapter","Pl","BlockPackTemplateField","InitialBlockSettings","stagingGraph","productionGraph","allBlocks","projectFieldName","BLOCK_STORAGE_FACADE_VERSION","createBContextFromUpstreams","createBContextEnd","exportContext","createRenderHeavyBlock","createBlockPack","FieldsToDuplicate","graphDiff","Denque","blockArgsAuthorKey","ProjectStructureAuthorKey","ProjectLastModifiedTimestamp","ProjectStructureKey","BlockRenderingStateKey","ProjectMetaKey","SchemaVersionKey","parseProjectField","SchemaVersionCurrent","UiError","getPreparedExportTemplateEnvelope","getServiceTemplateField","loadTemplate","InitialBlockMeta","ProjectResourceType","ProjectCreatedTimestamp","InitialBlockStructure","InitialProjectRenderingState","SchemaVersionV3","BlockArgsAuthorKeyPrefix","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 {\n createRenderHeavyBlock,\n createBContextFromUpstreams,\n createBContextEnd,\n} 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 SchemaVersionV3,\n ProjectResourceType,\n InitialBlockStructure,\n InitialProjectRenderingState,\n ProjectMetaKey,\n InitialBlockMeta,\n blockArgsAuthorKey,\n BlockArgsAuthorKeyPrefix,\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 { gzipSync } from \"node:zlib\";\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 value!;\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 if ((this.fields.prodOutput === undefined) !== (this.fields.prodUiCtx === undefined))\n throw new Error(\"inconsistent prod fields (prodUiCtx)\");\n\n if ((this.fields.stagingOutput === undefined) !== (this.fields.stagingCtx === undefined))\n throw new Error(\"inconsistent stage fields\");\n if ((this.fields.stagingOutput === undefined) !== (this.fields.stagingUiCtx === undefined))\n throw new Error(\"inconsistent stage fields (stagingUiCtx)\");\n\n if (\n (this.fields.prodOutputPrevious === undefined) !==\n (this.fields.prodCtxPrevious === undefined)\n )\n throw new Error(\"inconsistent prod cache fields\");\n if (\n (this.fields.prodOutputPrevious === undefined) !==\n (this.fields.prodUiCtxPrevious === undefined)\n )\n throw new Error(\"inconsistent prod cache fields (prodUiCtxPrevious)\");\n\n if (\n (this.fields.stagingOutputPrevious === undefined) !==\n (this.fields.stagingCtxPrevious === undefined)\n )\n throw new Error(\"inconsistent stage cache fields\");\n if (\n (this.fields.stagingOutputPrevious === undefined) !==\n (this.fields.stagingUiCtxPrevious === undefined)\n )\n throw new Error(\"inconsistent stage cache fields (stagingUiCtxPrevious)\");\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 /** Max number of blocks to render staging for in a single background refresh pass. */\n private static readonly STAGING_REFRESH_MAX_BATCH = 10;\n\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 // All four fields (prodArgs, prodOutput, prodCtx, prodUiCtx) must be present together.\n // prodUiCtx can be missing after project duplication: prodCtx uses a holder wrapper\n // (always non-null), but prodUiCtx is a raw FieldRef that may still be NullResourceId\n // at snapshot time and thus not copied.\n this.blockInfos.forEach((blockInfo) => {\n if (\n blockInfo.fields.prodArgs === undefined ||\n blockInfo.fields.prodOutput === undefined ||\n blockInfo.fields.prodCtx === undefined ||\n blockInfo.fields.prodUiCtx === undefined\n )\n this.deleteBlockFields(blockInfo.id, \"prodArgs\", \"prodOutput\", \"prodCtx\", \"prodUiCtx\");\n });\n\n // Fix inconsistent staging fields (same FieldRef issue for stagingUiCtx)\n this.blockInfos.forEach((blockInfo) => {\n if (\n blockInfo.fields.stagingOutput === undefined ||\n blockInfo.fields.stagingCtx === undefined ||\n blockInfo.fields.stagingUiCtx === undefined\n )\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 blockInfo.fields.prodUiCtxPrevious === 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 blockInfo.fields.stagingUiCtxPrevious === 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 // Migration: build production context chain if not present,\n // and reset all stagings so they re-render using the new chain\n const needsChainBuild = [...this.blockInfos.values()].some(\n (info) => info.fields.prodChainCtx === undefined,\n );\n if (needsChainBuild && this.blockInfos.size > 0) {\n this.rebuildProdChain(0);\n this.blockInfos.forEach((blockInfo) => {\n this.resetStaging(blockInfo.id);\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 /** Create a plain JSON resource value from a JS object (no compression). */\n private createJsonFieldValue(obj: unknown): BlockFieldStateValue {\n const value = Buffer.from(JSON.stringify(obj));\n const ref = this.tx.createValue(Pl.JsonObject, value);\n return { ref, value, status: \"Ready\" };\n }\n\n /** Create a plain JSON resource value from a pre-serialized JSON string (no compression). */\n private createJsonFieldValueFromContent(json: string): BlockFieldStateValue {\n const value = Buffer.from(json);\n const ref = this.tx.createValue(Pl.JsonObject, value);\n return { ref, value, status: \"Ready\" };\n }\n\n /** Create a gzip-compressed JSON resource value from a JS object (compressed if >= 16KB). */\n private createGzJsonFieldValue(obj: unknown): BlockFieldStateValue {\n const jsonBytes = canonicalJsonBytes(obj);\n return this.createGzJsonFieldValueFromBytes(jsonBytes);\n }\n\n /** Create a gzip-compressed JSON resource value from a pre-serialized JSON string (compressed if >= 16KB). */\n private createGzJsonFieldValueFromContent(json: string): BlockFieldStateValue {\n return this.createGzJsonFieldValueFromBytes(Buffer.from(json));\n }\n\n private createGzJsonFieldValueFromBytes(jsonBytes: Uint8Array): BlockFieldStateValue {\n const gzipThreshold = 16_384;\n if (jsonBytes.length >= gzipThreshold) {\n const data = gzipSync(jsonBytes);\n const ref = this.tx.createValue(Pl.JsonGzObject, data);\n return { ref, value: data, status: \"Ready\" };\n }\n const ref = this.tx.createValue(Pl.JsonObject, jsonBytes);\n return { ref, value: jsonBytes, status: \"Ready\" };\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.createGzJsonFieldValueFromContent(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.createGzJsonFieldValueFromContent(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.createGzJsonFieldValue(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 // NB: currentArgs must NOT be gzipped — they are read by Tengo workflows which can't decompress\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 // NB: currentPrerunArgs must NOT be gzipped — they are read by Tengo workflows which can't decompress\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 // Render staging inline for blocks with changed prerunArgs — no downstream cascade.\n // Each block's staging uses only the pre-built production context chain (prodChainCtx),\n // which doesn't change on arg edits, so downstream blocks are unaffected.\n for (const blockId of changedArgs) {\n try {\n this.renderStagingFor(blockId);\n } catch (e) {\n this.projectHelper.logger.error(\n new Error(`[setStates] inline staging render failed for ${blockId}`, { cause: e }),\n );\n }\n }\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 /**\n * Rebuilds the production context chain from `fromBlockIndex` to the end.\n * Each block gets a `prodChainCtx` field containing the accumulated production\n * contexts from all blocks above it in project order.\n *\n * Construction rule for block at position K:\n * - If block K-1 has prodCtx: chainNode[K] = BContext(chainNode[K-1], K-1.prodCtx)\n * - If block K-1 has no prodCtx: chainNode[K] = chainNode[K-1] (passthrough)\n * - Block 0: chainNode[0] = BContextEnd\n */\n private rebuildProdChain(fromBlockIndex: number = 0): void {\n const blocks = [...allBlocks(this.struct)];\n if (fromBlockIndex >= blocks.length) return;\n\n // Get the inner chain context ref of the block before fromBlockIndex\n let prevChainCtx: AnyRef | undefined;\n if (fromBlockIndex > 0) {\n const prevHolder = this.getBlockInfo(blocks[fromBlockIndex - 1].id).fields.prodChainCtx?.ref;\n if (prevHolder === undefined) {\n throw new Error(\n `rebuildProdChain(${fromBlockIndex}): block ${blocks[fromBlockIndex - 1].id} at position ${fromBlockIndex - 1} has no prodChainCtx — chain must be built from 0 first`,\n );\n }\n prevChainCtx = Pl.unwrapHolder(this.tx, prevHolder);\n }\n\n for (let i = fromBlockIndex; i < blocks.length; i++) {\n const blockId = blocks[i].id;\n\n let newChainCtx: AnyRef;\n\n if (i === 0) {\n // First block: nothing above\n newChainCtx = createBContextEnd(this.tx);\n } else {\n const prevBlockId = blocks[i - 1].id;\n const prevInfo = this.getBlockInfo(prevBlockId);\n const prevProdCtxHolder = prevInfo.fields.prodCtx?.ref;\n\n if (prevProdCtxHolder !== undefined) {\n // Block above has production: accumulate into chain\n const upstreams: AnyRef[] = [];\n upstreams.push(prevChainCtx!);\n upstreams.push(Pl.unwrapHolder(this.tx, prevProdCtxHolder));\n newChainCtx = createBContextFromUpstreams(this.tx, upstreams);\n } else {\n // Passthrough: reuse inner context from previous block\n newChainCtx = prevChainCtx!;\n }\n }\n\n // Store chain node (wrapped in holder)\n this.setBlockField(\n blockId,\n \"prodChainCtx\",\n Pl.wrapInEphHolder(this.tx, newChainCtx),\n \"NotReady\",\n );\n\n prevChainCtx = newChainCtx;\n }\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 // Skip if currentPrerunArgs is not set (prerunArgs() returned undefined or args derivation failed)\n const prerunArgsRef = info.fields.currentPrerunArgs?.ref;\n if (prerunArgsRef === undefined) {\n return;\n }\n\n this.resetStaging(blockId);\n\n // Use the pre-built production context chain node\n const chainCtxHolder = info.fields.prodChainCtx?.ref;\n if (chainCtxHolder === undefined) {\n throw new Error(\n `[renderStagingFor] block ${blockId} has no prodChainCtx — chain must be built before staging render`,\n );\n }\n const ctx = Pl.unwrapHolder(this.tx, chainCtxHolder);\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.createGzJsonFieldValueFromContent(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 // Rebuild production chain — structure (and thus block order) may have changed.\n // Find the first position that changed to avoid rebuilding the entire chain.\n const newBlocks = [...allBlocks(newStructure)];\n const oldBlocks = [...currentStagingGraph.nodes.keys()];\n const n = Math.min(newBlocks.length, oldBlocks.length);\n let firstChangedIdx = 0;\n while (firstChangedIdx < n && newBlocks[firstChangedIdx].id === oldBlocks[firstChangedIdx]) {\n firstChangedIdx++;\n }\n if (firstChangedIdx < newBlocks.length) {\n this.rebuildProdChain(firstChangedIdx);\n }\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\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 // Rebuild chain from this block onward (production may have been reset/limboed)\n // and reset staging for the migrated block + everything below\n const blocksList = [...allBlocks(this.struct)];\n const blockIdx = blocksList.findIndex((b) => b.id === blockId);\n if (blockIdx >= 0) {\n this.rebuildProdChain(blockIdx + 1);\n for (let i = blockIdx; i < blocksList.length; i++) {\n this.resetStaging(blocksList[i].id);\n }\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 // Rebuild production chain and reset downstream staging\n if (rendered.size > 0) {\n const blocksList = [...allBlocks(this.struct)];\n const firstRenderedIdx = blocksList.findIndex((b) => rendered.has(b.id));\n if (firstRenderedIdx >= 0) {\n // Chain from firstRenderedIdx+1 onward changes (rendered blocks have new prodCtx)\n this.rebuildProdChain(firstRenderedIdx + 1);\n // Reset staging for all blocks after the first rendered block\n // (their chain node changed; the first rendered block's chain is unaffected)\n for (let i = firstRenderedIdx + 1; i < blocksList.length; i++) {\n this.resetStaging(blocksList[i].id);\n }\n }\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 // Rebuild chain and reset staging for stopped blocks and everything below\n if (stopped.length > 0) {\n const blocksList = [...allBlocks(this.struct)];\n const stoppedSet = new Set(stopped);\n const firstStoppedIdx = blocksList.findIndex((b) => stoppedSet.has(b.id));\n if (firstStoppedIdx >= 0) {\n // Stopped blocks lost prodCtx — chain below them changes\n this.rebuildProdChain(firstStoppedIdx + 1);\n for (let i = firstStoppedIdx; i < blocksList.length; i++) {\n this.resetStaging(blocksList[i].id);\n }\n }\n }\n }\n\n /** Renders staging for blocks that need it (have currentPrerunArgs but no staging). */\n private refreshStagings() {\n const maxBatch = ProjectMutator.STAGING_REFRESH_MAX_BATCH;\n let rendered = 0;\n for (const block of allBlocks(this.struct)) {\n if (rendered >= maxBatch) break;\n const info = this.getBlockInfo(block.id);\n if (info.requireStagingRendering) {\n try {\n this.renderStagingFor(block.id);\n rendered++;\n } catch (e) {\n this.projectHelper.logger.error(\n new Error(`[refreshStagings] renderStagingFor failed for ${block.id}`, { 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 /** Background maintenance: render pending stagings + GC of Previous fields. */\n public doRefresh() {\n this.refreshStagings();\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\n/**\n * Duplicates an existing project resource within a transaction.\n * Creates a new project resource with all dynamic fields (block data) copied by reference\n * and all KV metadata copied with appropriate adjustments.\n *\n * The returned resource is NOT attached to any project list — the caller is responsible\n * for placing it where needed. This enables cross-user duplication.\n *\n * @param tx - active write transaction\n * @param sourceRid - resource id of the project to duplicate\n * @param options.label - optional label override for the new project; if omitted, copies the source label\n */\nexport async function duplicateProject(\n tx: PlTransaction,\n sourceRid: ResourceId,\n options?: { label?: string },\n): Promise<AnyResourceRef> {\n // Read source resource data (with fields) and all KV pairs\n const sourceDataP = tx.getResourceData(sourceRid, true);\n const sourceKVsP = tx.listKeyValuesString(sourceRid);\n\n const sourceData = await sourceDataP;\n const sourceKVs = await sourceKVsP;\n\n // Validate schema version (accept current and previous version that can be migrated on open)\n const schemaKV = sourceKVs.find((kv) => kv.key === SchemaVersionKey);\n const schema = schemaKV ? JSON.parse(schemaKV.value) : undefined;\n if (schema !== SchemaVersionCurrent && schema !== SchemaVersionV3) {\n throw new UiError(\n `Cannot duplicate project with schema version ${schema ?? \"unknown\"}. ` +\n `Only schema versions ${SchemaVersionV3} and ${SchemaVersionCurrent} are supported. ` +\n `Try opening the project first to trigger migration.`,\n );\n }\n\n // Create new project resource\n const newPrj = tx.createEphemeral(ProjectResourceType);\n tx.lock(newPrj);\n\n // Copy KV pairs with adjustments\n const ts = String(Date.now());\n const kvSkipPrefixes = [BlockArgsAuthorKeyPrefix];\n const kvSkipKeys = new Set([\n ProjectCreatedTimestamp,\n ProjectLastModifiedTimestamp,\n ProjectStructureAuthorKey,\n ]);\n\n for (const { key, value } of sourceKVs) {\n // Skip author markers\n if (kvSkipKeys.has(key)) continue;\n if (kvSkipPrefixes.some((prefix) => key.startsWith(prefix))) continue;\n\n if (key === ProjectMetaKey && options?.label !== undefined) {\n // Override label\n const meta: ProjectMeta = JSON.parse(value);\n tx.setKValue(newPrj, key, JSON.stringify({ ...meta, label: options.label }));\n } else {\n // Copy as-is\n tx.setKValue(newPrj, key, value);\n }\n }\n\n // Set fresh timestamps\n tx.setKValue(newPrj, ProjectCreatedTimestamp, ts);\n tx.setKValue(newPrj, ProjectLastModifiedTimestamp, ts);\n\n // Copy only persistent block fields (FieldsToDuplicate).\n // Transient fields (prodChainCtx, staging*, *Previous) are rebuilt on project open\n // by fixProblemsAndMigrate(). Copying them is both unnecessary and dangerous:\n // some fields use raw FieldRefs (not holder-wrapped) whose values may be NullResourceId\n // at snapshot time, leading to partially copied field groups and broken project state.\n for (const f of sourceData.fields) {\n if (isNullResourceId(f.value)) continue;\n const parsed = parseProjectField(f.name);\n if (parsed !== undefined && !FieldsToDuplicate.has(parsed.fieldName)) continue;\n tx.createField(field(newPrj, f.name), \"Dynamic\", f.value);\n }\n\n return newPrj;\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":";;;;;;;;;;;;;;;;;AAwGA,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;;;AAIX,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;AAC7C,MAAK,KAAK,OAAO,eAAe,YAAgB,KAAK,OAAO,cAAc,QACxE,OAAM,IAAI,MAAM,uCAAuC;AAEzD,MAAK,KAAK,OAAO,kBAAkB,YAAgB,KAAK,OAAO,eAAe,QAC5E,OAAM,IAAI,MAAM,4BAA4B;AAC9C,MAAK,KAAK,OAAO,kBAAkB,YAAgB,KAAK,OAAO,iBAAiB,QAC9E,OAAM,IAAI,MAAM,2CAA2C;AAE7D,MACG,KAAK,OAAO,uBAAuB,YACnC,KAAK,OAAO,oBAAoB,QAEjC,OAAM,IAAI,MAAM,iCAAiC;AACnD,MACG,KAAK,OAAO,uBA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,eAAe,QAAQ,KAAK,OAAO;EACzE,MAAM,SAAS,MAAM,GAAG,IAAI;AAC5B,MAAI,MAAM;AACV,SAAO"}
|
package/dist/mutator/project.js
CHANGED
|
@@ -29,7 +29,7 @@ function cached(modIdCb, valueCb) {
|
|
|
29
29
|
lastModId = currentModId;
|
|
30
30
|
value = valueCb();
|
|
31
31
|
}
|
|
32
|
-
return
|
|
32
|
+
return value;
|
|
33
33
|
};
|
|
34
34
|
}
|
|
35
35
|
var BlockInfo = class {
|
|
@@ -42,9 +42,13 @@ var BlockInfo = class {
|
|
|
42
42
|
}
|
|
43
43
|
check() {
|
|
44
44
|
if (this.fields.prodOutput === void 0 !== (this.fields.prodCtx === void 0)) throw new Error("inconsistent prod fields");
|
|
45
|
+
if (this.fields.prodOutput === void 0 !== (this.fields.prodUiCtx === void 0)) throw new Error("inconsistent prod fields (prodUiCtx)");
|
|
45
46
|
if (this.fields.stagingOutput === void 0 !== (this.fields.stagingCtx === void 0)) throw new Error("inconsistent stage fields");
|
|
47
|
+
if (this.fields.stagingOutput === void 0 !== (this.fields.stagingUiCtx === void 0)) throw new Error("inconsistent stage fields (stagingUiCtx)");
|
|
46
48
|
if (this.fields.prodOutputPrevious === void 0 !== (this.fields.prodCtxPrevious === void 0)) throw new Error("inconsistent prod cache fields");
|
|
49
|
+
if (this.fields.prodOutputPrevious === void 0 !== (this.fields.prodUiCtxPrevious === void 0)) throw new Error("inconsistent prod cache fields (prodUiCtxPrevious)");
|
|
47
50
|
if (this.fields.stagingOutputPrevious === void 0 !== (this.fields.stagingCtxPrevious === void 0)) throw new Error("inconsistent stage cache fields");
|
|
51
|
+
if (this.fields.stagingOutputPrevious === void 0 !== (this.fields.stagingUiCtxPrevious === void 0)) throw new Error("inconsistent stage cache fields (stagingUiCtxPrevious)");
|
|
48
52
|
if (this.fields.blockPack === void 0) throw new Error("no block pack field");
|
|
49
53
|
if (this.fields.blockStorage === void 0) throw new Error("no block storage field");
|
|
50
54
|
}
|
|
@@ -145,14 +149,14 @@ var ProjectMutator = class ProjectMutator {
|
|
|
145
149
|
}
|
|
146
150
|
fixProblemsAndMigrate() {
|
|
147
151
|
this.blockInfos.forEach((blockInfo) => {
|
|
148
|
-
if (blockInfo.fields.prodArgs === void 0 || blockInfo.fields.prodOutput === void 0 || blockInfo.fields.prodCtx === void 0) this.deleteBlockFields(blockInfo.id, "prodArgs", "prodOutput", "prodCtx", "prodUiCtx");
|
|
152
|
+
if (blockInfo.fields.prodArgs === void 0 || blockInfo.fields.prodOutput === void 0 || blockInfo.fields.prodCtx === void 0 || blockInfo.fields.prodUiCtx === void 0) this.deleteBlockFields(blockInfo.id, "prodArgs", "prodOutput", "prodCtx", "prodUiCtx");
|
|
149
153
|
});
|
|
150
154
|
this.blockInfos.forEach((blockInfo) => {
|
|
151
|
-
if (blockInfo.fields.stagingOutput === void 0 || blockInfo.fields.stagingCtx === void 0) this.deleteBlockFields(blockInfo.id, "stagingOutput", "stagingCtx", "stagingUiCtx");
|
|
155
|
+
if (blockInfo.fields.stagingOutput === void 0 || blockInfo.fields.stagingCtx === void 0 || blockInfo.fields.stagingUiCtx === void 0) this.deleteBlockFields(blockInfo.id, "stagingOutput", "stagingCtx", "stagingUiCtx");
|
|
152
156
|
});
|
|
153
157
|
this.blockInfos.forEach((blockInfo) => {
|
|
154
|
-
if (blockInfo.fields.prodOutputPrevious === void 0 || blockInfo.fields.prodCtxPrevious === void 0) this.deleteBlockFields(blockInfo.id, "prodOutputPrevious", "prodCtxPrevious", "prodUiCtxPrevious");
|
|
155
|
-
if (blockInfo.fields.stagingOutputPrevious === void 0 || blockInfo.fields.stagingCtxPrevious === void 0) this.deleteBlockFields(blockInfo.id, "stagingOutputPrevious", "stagingCtxPrevious", "stagingUiCtxPrevious");
|
|
158
|
+
if (blockInfo.fields.prodOutputPrevious === void 0 || blockInfo.fields.prodCtxPrevious === void 0 || blockInfo.fields.prodUiCtxPrevious === void 0) this.deleteBlockFields(blockInfo.id, "prodOutputPrevious", "prodCtxPrevious", "prodUiCtxPrevious");
|
|
159
|
+
if (blockInfo.fields.stagingOutputPrevious === void 0 || blockInfo.fields.stagingCtxPrevious === void 0 || blockInfo.fields.stagingUiCtxPrevious === void 0) this.deleteBlockFields(blockInfo.id, "stagingOutputPrevious", "stagingCtxPrevious", "stagingUiCtxPrevious");
|
|
156
160
|
});
|
|
157
161
|
let initialBlockSettings;
|
|
158
162
|
this.blockInfos.forEach((blockInfo) => {
|
|
@@ -1030,7 +1034,12 @@ async function duplicateProject(tx, sourceRid, options) {
|
|
|
1030
1034
|
}
|
|
1031
1035
|
tx.setKValue(newPrj, ProjectCreatedTimestamp, ts);
|
|
1032
1036
|
tx.setKValue(newPrj, ProjectLastModifiedTimestamp, ts);
|
|
1033
|
-
for (const f of sourceData.fields)
|
|
1037
|
+
for (const f of sourceData.fields) {
|
|
1038
|
+
if (isNullResourceId(f.value)) continue;
|
|
1039
|
+
const parsed = parseProjectField(f.name);
|
|
1040
|
+
if (parsed !== void 0 && !FieldsToDuplicate.has(parsed.fieldName)) continue;
|
|
1041
|
+
tx.createField(field(newPrj, f.name), "Dynamic", f.value);
|
|
1042
|
+
}
|
|
1034
1043
|
return newPrj;
|
|
1035
1044
|
}
|
|
1036
1045
|
async function withProject(projectHelper, txOrPl, rid, cb, ops) {
|