docs(canvas): explain streamed loading and certified scope admission

This commit is contained in:
2026-11-12 00:25:26 +08:00
parent b471d88520
commit 68e655b52c
6 changed files with 138 additions and 0 deletions
+35
View File
@@ -1,17 +1,25 @@
/**
* 作用域化只读加载调度器:同 query identity 合并进行中的请求,每个导航订阅仍有独立取消/快照。
* queue 控制等待、active 限并发、cache 可选且必须额外验证;不拥有 working/history/ACK,也不直接发布业务文档。
* 端口必须验证并提供不可变 payload 和测量 byteLength,这里校验捕获元数据、连续块与预算,不猜任意 T 的结构。
*/
import type { Id } from '../canvas-core/types.ts';
import { equal, fault, freeze } from '../canvas-core/values.ts';
/** 账号/工作区/项目/画布/查询/格式/来源/authEpoch 决定共享身份,navigationGeneration 属于订阅者。 */
export type LoadScope = {
readonly accountId: Id; readonly workspaceId: Id; readonly projectId: Id | null; readonly canvasId: Id;
readonly viewKey: string; readonly format: string; readonly queryFingerprint: string; readonly source: string;
readonly authEpoch: number; readonly navigationGeneration: number;
};
/** 一次捕获的连续块:queryKey/token/版本/权限代次固定,coverage 由协议适配器解释。 */
export type LoadChunk<T> = {
readonly queryKey: string; readonly projectId: Id; readonly snapshotToken: string;
readonly serverVersions: Readonly<Record<Id, number>>; readonly permissionEpoch: number;
readonly sequence: number; readonly coverage: readonly string[]; readonly complete: boolean;
readonly byteLength: number; readonly value: T;
};
/** 加载状态及已收到块;initial-ready/partial 只说明有内容,ready Promise 要到 terminal+EOF 才完成。 */
export type LoadSnapshot<T> = {
readonly scope: LoadScope; readonly generation: number;
readonly cacheStatus: 'bypassed' | 'miss' | 'invalid' | 'hit';
@@ -19,10 +27,13 @@ export type LoadSnapshot<T> = {
readonly chunks: readonly LoadChunk<T>[]; readonly coverage: readonly string[]; readonly byteLength: number;
readonly error?: { readonly code: string; readonly message: string };
};
/** 可取消的异步只读块源,负责实际 HTTP/解码与 payload 校验,不能在这里偷偷写项目状态。 */
export type LoadPort<T> = (scope: LoadScope, signal: AbortSignal) => AsyncIterable<LoadChunk<T>>;
/** 单订阅句柄:ready 等完整读取,getSnapshot 可看合法前缀,release 仅解除自己的借用。 */
export type LoadHandle<T> = { readonly ready: Promise<LoadSnapshot<T>>; getSnapshot(): LoadSnapshot<T>; release(): void };
/** Navigation belongs to a subscriber, not the shared query identity. */
/** 验证 scope 并稳定编码共享 key;刻意排除导航代次,让两个 view 可复用同一读取。 */
export function loadQueryKey(scope: LoadScope): string {
if (!scope || ![scope.accountId, scope.workspaceId, scope.canvasId, scope.viewKey, scope.format, scope.queryFingerprint, scope.source].every(value => typeof value === 'string' && value.length > 0)
|| scope.projectId !== null && (typeof scope.projectId !== 'string' || !scope.projectId)
@@ -33,6 +44,7 @@ export function loadQueryKey(scope: LoadScope): string {
/** Read copies only. This coordinator never publishes a document or a server ACK.
* Adapters validate payloads and declare their measured byte cost before yielding.
*/
/** 装配有界加载池;默认缓存年龄0不保留空闲结果,启用缓存也要求宿主 acceptCached 明确同意。 */
export function createScopedLoader<T>(options: {
load: LoadPort<T>; current(scope: LoadScope): boolean; concurrency?: number; maxQueued?: number;
maxResponseBytes?: number; maxChunks?: number; maxCacheEntries?: number; maxCacheBytes?: number;
@@ -40,23 +52,34 @@ export function createScopedLoader<T>(options: {
acceptCached?(scope: LoadScope, snapshot: LoadSnapshot<T>): boolean;
onObserverError?(error: unknown): void;
}) {
/** 订阅自己的 scope/取消/通知状态,不把旧导航的取消写成整个 job 失败。 */
type Subscriber = { readonly scope: LoadScope; active: boolean; cancelled?: LoadSnapshot<T>; lastState?: LoadSnapshot<T>; snapshot?: LoadSnapshot<T>;resolve(value: LoadSnapshot<T>): void; reject(error: unknown): void; cleanup(): void; notify?(snapshot: LoadSnapshot<T>): void };
/** 共享 query 的执行句柄;generation 防重开混淆,controller 只在最后订阅释放时 abort。 */
type Job = { readonly key: string; readonly scope: LoadScope; readonly generation: number; readonly controller: AbortController; readonly subscribers: Set<Subscriber>; state: LoadSnapshot<T>; retired: boolean };
const concurrency = options.concurrency ?? 4, maxQueued = options.maxQueued ?? 128, maxBytes = options.maxResponseBytes ?? 64 * 1024 * 1024, maxChunks = options.maxChunks ?? 4096;
const maxCacheEntries = options.maxCacheEntries ?? 16, maxCacheBytes = options.maxCacheBytes ?? 16 * 1024 * 1024, cacheMaxAge = options.cacheMaxAgeMs ?? 0;
for (const [name, value] of Object.entries({ concurrency, maxQueued, maxBytes, maxChunks, maxCacheEntries, maxCacheBytes, cacheMaxAge })) if (!Number.isSafeInteger(value) || value < (['maxCacheEntries', 'maxCacheBytes', 'cacheMaxAge'].includes(name) ? 0 : 1)) throw fault('invalid_loader_budget');
// inflight 按 query 去重,queue 按 generation FIFO;cache 的 Map 插入顺序用于 LRU 淘汰。
const inflight = new Map<string, Job>(), queue = new Map<number, Job>(), cache = new Map<string, { state: LoadSnapshot<T>; at: number }>();
let active = 0, generation = 0, closed = false, cacheBytes = 0;
/** 缓存计时输入必须非负安全整数;具体缓存命中还要检查时间倒退/年龄。 */
const now = () => { const value = (options.now ?? Date.now)(); if (!Number.isSafeInteger(value) || value < 0) throw fault('invalid_loader_clock'); return value; };
/** 每个异步采纳边界检查 loader 生命周期与精确身份,不用 HTTP 成功替代授权仍有效。 */
const current = (scope: LoadScope) => { if (closed) throw fault('loader_closed'); if (!options.current(scope)) throw fault('identity_changed'); };
/** 共享 job 状态不变时复用该订阅快照;scope 替换为订阅者自己的导航信息。 */
const view = (job: Job, subscriber: Subscriber): LoadSnapshot<T> => {if(subscriber.lastState!==job.state){subscriber.lastState=job.state;subscriber.snapshot=Object.freeze({...job.state,scope:subscriber.scope});}return subscriber.snapshot!;};
/** 单独隔离 observer 异常,报告失败也不改变共享执行和其它订阅者的通知。 */
const notifyOne = (job: Job, subscriber: Subscriber) => { if (subscriber.active) { try { subscriber.notify?.(view(job, subscriber)); } catch (error) { try { options.onObserverError?.(error); } catch { /* Observers cannot change scheduling. */ } } } };
/** 副本遍历当前订阅者,允许通知过程中某订阅自行释放而不破坏迭代。 */
const notify = (job: Job) => { for (const subscriber of [...job.subscribers]) notifyOne(job, subscriber); };
/** 淘汰同时扣除字节计数,避免缓存键删了但容量永远不归还。 */
const removeCache = (key: string) => { const entry = cache.get(key); if (entry) { cacheBytes -= entry.state.byteLength; cache.delete(key); } };
/** 幂等废弃 job:abort/出等待队列,仅当 inflight 仍指向自己时删除,旧任务不能删新代次。 */
function retire(job: Job) {
if (job.retired) return; job.retired = true; job.controller.abort(); queue.delete(job.generation);
if (inflight.get(job.key) === job) inflight.delete(job.key);
}
/** FIFO 启动有订阅的工作并登记真实 active;即使源无视 abort,只有它 finally 结束才归还槽位。 */
function pump() {
while (!closed && active < concurrency && queue.size) {
const job = queue.values().next().value!; queue.delete(job.generation);
@@ -65,6 +88,7 @@ export function createScopedLoader<T>(options: {
void (async () => {
try {
current(job.scope); let terminal = false, projectId: Id | undefined, token: string | undefined, versions: Readonly<Record<Id, number>> | undefined;
// 每块重新检查身份并核对连续序号、稳定 token/项目/版本向量、权限代次和声明预算。
for await (const chunk of options.load(job.scope, job.controller.signal)) {
if (job.retired) return; current(job.scope);
if (!chunk || terminal || chunk.queryKey !== job.key || chunk.permissionEpoch !== job.scope.authEpoch || typeof chunk.projectId !== 'string' || !chunk.projectId
@@ -74,17 +98,20 @@ export function createScopedLoader<T>(options: {
|| !chunk.serverVersions || typeof chunk.serverVersions !== 'object' || Array.isArray(chunk.serverVersions)
|| ![Object.prototype,null].includes(Object.getPrototypeOf(chunk.serverVersions))
|| Object.entries(chunk.serverVersions).some(([id, version]) => !id || !Number.isSafeInteger(version) || version < 1)) throw fault('invalid_load_chunk');
// 版本表顺序不影响相等性;同一捕获的内容版本发生变化则拒绝拼成完整结果。
const orderedVersions = Object.fromEntries(Object.entries(chunk.serverVersions).sort(([a], [b]) => a.localeCompare(b)));
if (projectId !== undefined && (chunk.projectId !== projectId || chunk.snapshotToken !== token || !equal(orderedVersions, versions))) throw fault('load_snapshot_changed');
if (job.state.chunks.length >= maxChunks || chunk.byteLength > maxBytes - job.state.byteLength) throw fault('load_response_budget');
projectId = chunk.projectId; token = chunk.snapshotToken; versions = orderedVersions; terminal = chunk.complete;
// Own metadata; the adapter supplies an immutable/owned payload value.
const owned = Object.freeze({ ...chunk, serverVersions: freeze(orderedVersions), coverage: freeze([...chunk.coverage]) });
// 只追加合法块并通知可显示前缀;即使当前块 complete=true,也要等源 EOF 才能成功 ready。
job.state = Object.freeze({ ...job.state, status: job.state.chunks.length ? 'partial' : 'initial-ready', chunks: Object.freeze([...job.state.chunks, owned]),
coverage: freeze([...new Set([...job.state.coverage, ...chunk.coverage])]), byteLength: job.state.byteLength + chunk.byteLength }); notify(job);
}
if (job.retired) return; current(job.scope); if (!terminal) throw fault('incomplete_load');
job.state = Object.freeze({ ...job.state, status: 'complete' });
// 完整结果才进缓存,且三项缓存开关都开启;按条数和测量字节双重 LRU 上限淘汰。
if (maxCacheEntries && maxCacheBytes && cacheMaxAge && job.state.byteLength <= maxCacheBytes) {
removeCache(job.key); cache.set(job.key, { state: job.state, at: now() }); cacheBytes += job.state.byteLength;
while (cache.size > maxCacheEntries || cacheBytes > maxCacheBytes) removeCache(cache.keys().next().value!);
@@ -92,10 +119,12 @@ export function createScopedLoader<T>(options: {
notify(job); for (const subscriber of job.subscribers) if (subscriber.active) { subscriber.cleanup(); subscriber.resolve(view(job, subscriber)); }
} catch (error) {
if (job.retired) return;
// 尾部失败仍保留已校验 chunks,状态 failed-with-partial;不伪造 complete,也不清掉已显示前缀。
const failure = error as { code?: string; message?: string };
job.state = Object.freeze({ ...job.state, status: job.state.chunks.length ? 'failed-with-partial' : 'failed', error: freeze({ code: typeof failure?.code === 'string' ? failure.code : 'load_failed', message: typeof failure?.message === 'string' ? failure.message : 'Load failed' }) });
notify(job); for (const subscriber of job.subscribers) if (subscriber.active) { subscriber.cleanup(); subscriber.reject(error); }
} finally {
// 迟到旧源结束时只清自己,不能误删相同 key 新订阅创建的 job;随后归还 active 并泵队列。
if (inflight.get(job.key) === job) inflight.delete(job.key);
active--; pump();
}
@@ -103,12 +132,14 @@ export function createScopedLoader<T>(options: {
}
}
return Object.freeze({
/** 捕获 scope→检查缓存→共享/新建 job→建立独立订阅;本调用不直接打开/关闭 ProjectEngine。 */
acquire(scopeInput: LoadScope, settings: { signal?: AbortSignal; onUpdate?: (snapshot: LoadSnapshot<T>) => void; cache?: 'fresh' | 'allow-validated' } = {}): LoadHandle<T> {
if(!scopeInput)throw fault('invalid_load_scope');
const scope:LoadScope=Object.freeze({accountId:scopeInput.accountId,workspaceId:scopeInput.workspaceId,projectId:scopeInput.projectId,canvasId:scopeInput.canvasId,viewKey:scopeInput.viewKey,format:scopeInput.format,queryFingerprint:scopeInput.queryFingerprint,source:scopeInput.source,authEpoch:scopeInput.authEpoch,navigationGeneration:scopeInput.navigationGeneration});
const key = loadQueryKey(scope); current(scope); if (settings.signal?.aborted) throw fault('load_aborted');
let cacheStatus: LoadSnapshot<T>['cacheStatus'] = settings.cache === 'allow-validated' ? 'miss' : 'bypassed';
let cached = cache.get(key);
// 缓存需 allow-validated、年龄有效且 acceptCached===true;时钟倒退或 fresh 都使旧缓存失效。
if (cached) {
const age = now() - cached.at;
if (age < 0 || age > cacheMaxAge || settings.cache !== 'allow-validated' || !options.acceptCached || options.acceptCached(scope, cached.state) !== true) { if(settings.cache==='allow-validated')cacheStatus='invalid';removeCache(key); cached = undefined; }
@@ -116,6 +147,7 @@ export function createScopedLoader<T>(options: {
}
current(scope);
if(settings.signal?.aborted)throw fault('load_aborted');
// 已有 inflight 优先共享;新 job 的容量包含可立即开跑的空槽,generation 溢出明确拒绝。
let job = inflight.get(key);
if (!job) {
if (!cached && queue.size >= maxQueued + Math.max(0, concurrency - active)) throw fault('load_capacity');
@@ -126,11 +158,13 @@ export function createScopedLoader<T>(options: {
}
const shared = job; let subscriber: Subscriber;
const ready = new Promise<LoadSnapshot<T>>((resolve, reject) => { subscriber = { scope, active: true, resolve, reject, notify: settings.onUpdate, cleanup: () => settings.signal?.removeEventListener('abort', release) }; });
/** 冻结本订阅 cancelled 视图、清监听并拒绝等待;只有最后借用者离开才 retire 共享源。 */
const release = () => { if (!subscriber.active) return; subscriber.cancelled=Object.freeze({...view(shared,subscriber),status:'cancelled'});subscriber.active = false; subscriber.cleanup(); shared.subscribers.delete(subscriber); subscriber.reject(fault('load_aborted')); if (!shared.subscribers.size) retire(shared); };
shared.subscribers.add(subscriber!); settings.signal?.addEventListener('abort', release, { once: true });
if (cached) { subscriber!.resolve(view(shared, subscriber!)); notifyOne(shared, subscriber!); } else { notifyOne(shared, subscriber!); pump(); }
return Object.freeze({ ready, getSnapshot: () => subscriber.cancelled??view(shared,subscriber), release });
},
/** 永久关闭并拒绝所有活订阅,清等待/缓存;底层迟到结果由 retired/current 围栏拒绝采纳。 */
close() {
if (closed) return; closed = true;
for (const job of [...inflight.values()]) { retire(job); for (const subscriber of job.subscribers) if (subscriber.active) { subscriber.cancelled=Object.freeze({...view(job,subscriber),status:'cancelled'});subscriber.active = false; subscriber.cleanup(); subscriber.reject(fault('loader_closed')); } job.subscribers.clear(); }
@@ -138,4 +172,5 @@ export function createScopedLoader<T>(options: {
},
});
}
/** 公共泛型读取器 API 由工厂推导,加载结果仍需协议层转换为具体 canonical 数据。 */
export type ScopedLoader<T> = ReturnType<typeof createScopedLoader<T>>;