Coordinate scoped model reads with bounded streaming state and identity fences
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import type { CanvasRecord, Document, ElementRecord, Id, Json, OperationIntent, Registry } from '../canvas-core/types.ts';
|
||||
import { createDocument, validateDocument } from '../canvas-core/document.ts';
|
||||
import { equal, fault, freeze, readMap, validJson } from '../canvas-core/values.ts';
|
||||
import type { AppliedModelProof, EngineSnapshot, PersistencePort } from '../canvas-runtime/contracts.ts';
|
||||
import type { AppliedModelProof, CanonicalReadResult, EngineSnapshot, PersistencePort } from '../canvas-runtime/contracts.ts';
|
||||
import type { FrozenRequest } from '../canvas-runtime/ledger.ts';
|
||||
import { projectBatch } from '../canvas-runtime/batch-projection.ts';
|
||||
import type { OperationStatus } from '../canvas-runtime/scheduler.ts';
|
||||
@@ -62,18 +62,20 @@ function modelChunk(value: unknown): ModelChunk {
|
||||
if (new Set(removed).size !== removed.length || removed.some(value => canvasIds.has(value) || !serverVersions.has(value)) || serverVersions.size !== canvasIds.size + removed.length) throw fault('invalid_canonical_response');
|
||||
return freeze({ projectId, complete: raw.complete, canvases, elements, tombstones: readMap(tombstones), versions: serverVersions, removed });
|
||||
}
|
||||
export type ModelLoad = { readonly status: 'ready'; readonly document: Document; readonly versions: ReadonlyMap<Id, number> } | { readonly status: 'migration-required'; readonly rootId: Id; readonly sourceVersions: ReadonlyMap<Id, number> };
|
||||
export type ModelLoad = CanonicalReadResult;
|
||||
export async function loadFoundryModel(transport: ServiceTransport, workspaceId: Id, canvasId: Id, registry: Registry, signal?: AbortSignal): Promise<ModelLoad> {
|
||||
const response = await transport({ method: 'GET', path: pathFor(workspaceId, canvasId), signal }), body = record(response.body);
|
||||
if (response.status === 409 && body.error === 'canonical_migration_required') {
|
||||
if (body.document_format_version !== 1) throw fault('unsupported_document_format');
|
||||
const rootId = id(body.root_id); if (!uuid.test(rootId)) throw fault('invalid_canonical_response');
|
||||
return freeze({ status: 'migration-required', rootId, sourceVersions: versions(body.source_versions) });
|
||||
const sourceVersions=versions(body.source_versions);if(!sourceVersions.has(canvasId)||!sourceVersions.has(rootId))throw fault('project_mismatch');
|
||||
return freeze({ status: 'migration-required', rootId, sourceVersions });
|
||||
}
|
||||
if (response.status !== 200) throw fault(typeof body.error === 'string' ? body.error : 'model_load_failed');
|
||||
const chunk = modelChunk(body.model);
|
||||
if (!chunk.complete) throw fault('incomplete_initial_model');
|
||||
const document = createDocument(chunk.projectId, chunk.canvases, chunk.elements, chunk.tombstones); validateDocument(document, registry);
|
||||
if(!document.canvases.has(canvasId))throw fault('project_mismatch');
|
||||
return Object.freeze({ status: 'ready', document, versions: chunk.versions });
|
||||
}
|
||||
|
||||
|
||||
@@ -2,3 +2,4 @@ export * from './foundry.ts';
|
||||
export * from './assets.ts';
|
||||
|
||||
export * from './runs.ts';
|
||||
export * from './model-loader.ts';
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
import type { Id, Registry } from '../canvas-core/types.ts';
|
||||
import { fault } from '../canvas-core/values.ts';
|
||||
import type { CanonicalReadResult } from '../canvas-runtime/contracts.ts';
|
||||
import { loadQueryKey, type LoadPort } from '../canvas-runtime/loader.ts';
|
||||
import { loadFoundryModel, type ServiceTransport } from './foundry.ts';
|
||||
|
||||
/** The current endpoint is complete-only. One validated chunk is intentional;
|
||||
* this adapter does not claim to stream an already-buffered response progressively.
|
||||
*/
|
||||
export function createFoundryModelLoader(options: { workspaceId: Id; registry: Registry; transport: ServiceTransport }): LoadPort<CanonicalReadResult> {
|
||||
return async function* (scope, signal) {
|
||||
if (scope.workspaceId !== options.workspaceId || scope.format !== 'canonical-model-v1' || scope.source !== 'foundry' || scope.viewKey !== 'canonical-project') throw fault('invalid_model_load_scope');
|
||||
const value = await loadFoundryModel(options.transport, options.workspaceId, scope.canvasId, options.registry, signal);
|
||||
signal.throwIfAborted();
|
||||
const projectId = value.status === 'ready' ? value.document.projectId : value.rootId;
|
||||
const versions = Object.fromEntries([...(value.status === 'ready' ? value.versions : value.sourceVersions)].sort(([a], [b]) => a.localeCompare(b)));
|
||||
const token = JSON.stringify([projectId, versions]);
|
||||
const payload = value.status === 'ready' ? { projectId, canvases: [...value.document.canvases.values()], elements: [...value.document.elements.values()], tombstones: [...value.document.tombstones], versions } : { projectId, versions, migrationRequired: true };
|
||||
// A serialized payload cost, not a claim about pre-parse network/heap limits.
|
||||
const byteLength = new TextEncoder().encode(JSON.stringify(payload)).byteLength;
|
||||
yield Object.freeze({ queryKey: loadQueryKey(scope), projectId, snapshotToken: token, serverVersions: Object.freeze(versions), permissionEpoch: scope.authEpoch,
|
||||
sequence: 0, coverage: Object.freeze(value.status === 'ready' ? [...value.document.canvases.keys()].map(id => `canvas:${id}`) : [`migration:${scope.canvasId}`]), complete: true, byteLength, value });
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user