feat(canvas): elide closed unsent lifetimes with durable local proofs

This commit is contained in:
2026-11-08 14:24:39 +08:00
parent 911931d265
commit 5296c953b1
12 changed files with 365 additions and 11 deletions
+16 -4
View File
@@ -15,6 +15,8 @@ export type SchedulerOptions = {
createId(): Id;
participants(intent: OperationIntent): readonly Id[];
compile(intent: OperationIntent, requestId: Id): CompiledRequest;
batchLimit?: (intents: readonly OperationIntent[]) => number;
elide?: (intents: readonly OperationIntent[]) => readonly Id[];
compileBatch?: (intents: readonly OperationIntent[], requestId: Id) => CompiledRequest;
submit(request: FrozenRequest, signal: AbortSignal): Promise<OperationStatus>;
query(request: FrozenRequest, signal: AbortSignal): Promise<OperationStatus>;
@@ -68,7 +70,7 @@ export function createScheduler(options: SchedulerOptions) {
/** Only a contiguous mutable document prefix may share a request. Task barriers stay visible. */
function batch(first: OperationIntent, canvases: Set<Id>): readonly OperationIntent[] {
const selected = [first], ids = new Set([first.id]);
if (!options.compileBatch || first.operationType !== 'document.change' || first.operationSchemaVersion !== 1) return selected;
if ((!options.compileBatch && !options.elide) || first.operationType !== 'document.change' || first.operationSchemaVersion !== 1) return selected;
let lo = 0, hi = ordered.length, changeCount = first.changes.length;
while (lo < hi) { const mid = (lo + hi) >>> 1; if (ordered[mid].sequence <= first.sequence) lo = mid + 1; else hi = mid; }
const ledger = options.getLedger();
@@ -85,7 +87,7 @@ export function createScheduler(options: SchedulerOptions) {
return selected;
}
function requestPump() {
if (!options.compileBatch) { pump(); return; }
if (!options.compileBatch && !options.elide) { pump(); return; }
if (!scheduledPump) scheduledPump = Promise.resolve().then(() => { scheduledPump = undefined; pump(); });
}
function own(request: FrozenRequest) {
@@ -185,8 +187,18 @@ export function createScheduler(options: SchedulerOptions) {
for (const canvas of occupied) { const set = waiters.get(canvas) ?? new Set<Id>(); set.add(id); waiters.set(canvas, set); }
continue;
}
const scope = new Set(canvases), intents = batch(record.intent, scope);
const participants = [...scope].sort();
const scope = new Set(canvases), selected = batch(record.intent, scope);
const limit = options.batchLimit?.(selected) ?? selected.length;
if (!Number.isSafeInteger(limit) || limit < 1 || limit > selected.length) throw fault('invalid_batch_limit');
const candidates = selected.slice(0, limit);
const elided = options.elide?.(candidates) ?? [];
if (elided.length) {
if (elided.some((id, index) => id !== candidates[index]?.id || options.getLedger().operations.get(id)?.resolution !== 'elided')) throw fault('invalid_elision');
for (const id of elided) for (const dependent of dependents.get(id) ?? []) enqueue(dependent);
continue;
}
const intents = options.compileBatch ? candidates : [record.intent];
const participants = [...new Set(intents.flatMap(intent => options.participants(intent)))].sort();
const requestId = options.createId(), compiled = intents.length > 1 ? options.compileBatch!(intents, requestId) : options.compile(record.intent, requestId);
if (participants.length !== compiled.canvasIds.length || participants.some(canvas => !compiled.canvasIds.includes(canvas))) throw fault('participant_mismatch');
options.mutate(ledger => ledger.freeze({ ...compiled, id: requestId, operationIds: intents.map(intent => intent.id) }));