Coordinate scoped model reads with bounded streaming state and identity fences

This commit is contained in:
2026-11-09 01:26:01 +08:00
parent 03c3a0dc66
commit 8219209284
13 changed files with 396 additions and 16 deletions
+24
View File
@@ -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 });
};
}