@agentshouse/mdruntime 0.1.0-cc294e382554d → 0.1.0-cc2e934017fb1
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/README.md +19 -16
- package/dist/acceptance.d.ts +3 -0
- package/dist/acceptance.d.ts.map +1 -0
- package/dist/acceptance.js +308 -0
- package/dist/actions.d.ts +20 -0
- package/dist/actions.d.ts.map +1 -0
- package/dist/actions.js +251 -0
- package/dist/adapter.d.ts +148 -122
- package/dist/adapter.d.ts.map +1 -1
- package/dist/attached-model.d.ts +7 -10
- package/dist/attached-model.d.ts.map +1 -1
- package/dist/attached-model.js +40 -41
- package/dist/binary.d.ts +0 -5
- package/dist/binary.d.ts.map +1 -1
- package/dist/binary.js +1 -1
- package/dist/bootstrap.d.ts +30 -11
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +309 -113
- package/dist/cell.d.ts +6 -0
- package/dist/cell.d.ts.map +1 -0
- package/dist/cell.js +9 -0
- package/dist/chain.d.ts +12 -0
- package/dist/chain.d.ts.map +1 -0
- package/dist/chain.js +64 -0
- package/dist/change-set.d.ts +10 -16
- package/dist/change-set.d.ts.map +1 -1
- package/dist/change-set.js +17 -94
- package/dist/change.d.ts +32 -1
- package/dist/change.d.ts.map +1 -1
- package/dist/change.js +14 -3
- package/dist/chat-types.d.ts +5 -0
- package/dist/chat-types.d.ts.map +1 -0
- package/dist/chat-types.js +13 -0
- package/dist/client.d.ts +33 -19
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +108 -17
- package/dist/collection.d.ts +29 -22
- package/dist/collection.d.ts.map +1 -1
- package/dist/collection.js +47 -354
- package/dist/compile.d.ts +16 -0
- package/dist/compile.d.ts.map +1 -0
- package/dist/compile.js +711 -0
- package/dist/connection.d.ts +7 -137
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +105 -3623
- package/dist/context.d.ts +2 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/{chat/context.js → context.js} +1 -4
- package/dist/conversation-live.d.ts +136 -0
- package/dist/conversation-live.d.ts.map +1 -0
- package/dist/conversation-live.js +249 -0
- package/dist/conversation-row.d.ts +33 -0
- package/dist/conversation-row.d.ts.map +1 -0
- package/dist/conversation-row.js +53 -0
- package/dist/conversation.d.ts +56 -46
- package/dist/conversation.d.ts.map +1 -1
- package/dist/conversation.js +964 -828
- package/dist/declaration.d.ts +10 -0
- package/dist/declaration.d.ts.map +1 -0
- package/dist/declaration.js +24 -0
- package/dist/digest.d.ts +7 -0
- package/dist/digest.d.ts.map +1 -0
- package/dist/digest.js +113 -0
- package/dist/discovery.d.ts +10 -0
- package/dist/discovery.d.ts.map +1 -0
- package/dist/discovery.js +148 -0
- package/dist/durability.d.ts +16 -4
- package/dist/durability.d.ts.map +1 -1
- package/dist/durability.js +5 -0
- package/dist/entry.d.ts +5 -2
- package/dist/entry.d.ts.map +1 -1
- package/dist/entry.js +33 -29
- package/dist/expectation.d.ts +4 -2
- package/dist/expectation.d.ts.map +1 -1
- package/dist/expectation.js +33 -11
- package/dist/failure.d.ts +1 -0
- package/dist/failure.d.ts.map +1 -1
- package/dist/failure.js +3 -0
- package/dist/figures.d.ts +49 -0
- package/dist/figures.d.ts.map +1 -0
- package/dist/figures.js +197 -0
- package/dist/file.d.ts +2 -19
- package/dist/file.d.ts.map +1 -1
- package/dist/file.js +7 -122
- package/dist/holdings.d.ts +29 -0
- package/dist/holdings.d.ts.map +1 -0
- package/dist/holdings.js +95 -0
- package/dist/host.d.ts +20 -8
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +99 -41
- package/dist/identity.d.ts +0 -2
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js +0 -6
- package/dist/index.d.ts +35 -13
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +14 -2
- package/dist/install.d.ts +54 -0
- package/dist/install.d.ts.map +1 -0
- package/dist/install.js +538 -0
- package/dist/lifecycle.d.ts +7 -0
- package/dist/lifecycle.d.ts.map +1 -0
- package/dist/lifecycle.js +272 -0
- package/dist/listing.d.ts +48 -0
- package/dist/listing.d.ts.map +1 -0
- package/dist/listing.js +139 -0
- package/dist/live.d.ts +23 -0
- package/dist/live.d.ts.map +1 -0
- package/dist/live.js +165 -0
- package/dist/locks.d.ts +7 -0
- package/dist/locks.d.ts.map +1 -0
- package/dist/locks.js +33 -0
- package/dist/log.d.ts +1 -0
- package/dist/log.d.ts.map +1 -1
- package/dist/lost.d.ts +5 -0
- package/dist/lost.d.ts.map +1 -0
- package/dist/lost.js +50 -0
- package/dist/media.d.ts +8 -0
- package/dist/media.d.ts.map +1 -0
- package/dist/media.js +218 -0
- package/dist/memory.d.ts +17 -7
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +257 -111
- package/dist/messages.d.ts +49 -0
- package/dist/messages.d.ts.map +1 -0
- package/dist/messages.js +150 -0
- package/dist/model.d.ts +8 -11
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +6 -23
- package/dist/models.d.ts +10 -0
- package/dist/models.d.ts.map +1 -0
- package/dist/models.js +156 -0
- package/dist/name.d.ts +4 -0
- package/dist/name.d.ts.map +1 -0
- package/dist/name.js +122 -0
- package/dist/observation.d.ts +15 -2
- package/dist/observation.d.ts.map +1 -1
- package/dist/observation.js +98 -14
- package/dist/opened.d.ts +22 -0
- package/dist/opened.d.ts.map +1 -0
- package/dist/opened.js +107 -0
- package/dist/opening.d.ts +4 -0
- package/dist/opening.d.ts.map +1 -0
- package/dist/opening.js +59 -0
- package/dist/outcome.d.ts +44 -9
- package/dist/outcome.d.ts.map +1 -1
- package/dist/outcome.js +41 -2
- package/dist/overlay.d.ts +4 -5
- package/dist/overlay.d.ts.map +1 -1
- package/dist/overlay.js +65 -169
- package/dist/page-metadata.d.ts +8 -0
- package/dist/page-metadata.d.ts.map +1 -0
- package/dist/page-metadata.js +19 -0
- package/dist/parts.d.ts +11 -0
- package/dist/parts.d.ts.map +1 -0
- package/dist/parts.js +21 -0
- package/dist/paths.d.ts +2 -0
- package/dist/paths.d.ts.map +1 -1
- package/dist/paths.js +26 -0
- package/dist/preparation.d.ts +20 -0
- package/dist/preparation.d.ts.map +1 -0
- package/dist/preparation.js +233 -0
- package/dist/presence.d.ts +52 -0
- package/dist/presence.d.ts.map +1 -0
- package/dist/presence.js +220 -0
- package/dist/queries.d.ts +16 -0
- package/dist/queries.d.ts.map +1 -0
- package/dist/queries.js +270 -0
- package/dist/query.d.ts +21 -207
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +3 -493
- package/dist/{chat/questions.d.ts → questions.d.ts} +5 -2
- package/dist/questions.d.ts.map +1 -0
- package/dist/{chat/questions.js → questions.js} +13 -2
- package/dist/queue.d.ts +40 -0
- package/dist/queue.d.ts.map +1 -0
- package/dist/queue.js +455 -0
- package/dist/reach.d.ts +20 -9
- package/dist/reach.d.ts.map +1 -1
- package/dist/reach.js +300 -106
- package/dist/react.d.ts +14 -2
- package/dist/react.d.ts.map +1 -1
- package/dist/react.js +10 -1
- package/dist/{chat/read.d.ts → read.d.ts} +2 -0
- package/dist/read.d.ts.map +1 -0
- package/dist/{chat/read.js → read.js} +4 -0
- package/dist/readiness.d.ts +13 -17
- package/dist/readiness.d.ts.map +1 -1
- package/dist/readiness.js +2 -8
- package/dist/reads.d.ts +36 -10
- package/dist/reads.d.ts.map +1 -1
- package/dist/reads.js +159 -83
- package/dist/recovery.d.ts +3 -2
- package/dist/recovery.d.ts.map +1 -1
- package/dist/recovery.js +43 -8
- package/dist/refresh.d.ts +18 -15
- package/dist/refresh.d.ts.map +1 -1
- package/dist/refresh.js +145 -79
- package/dist/refusal.d.ts +5 -0
- package/dist/refusal.d.ts.map +1 -0
- package/dist/refusal.js +57 -0
- package/dist/replica.d.ts +50 -39
- package/dist/replica.d.ts.map +1 -1
- package/dist/replica.js +319 -208
- package/dist/room-draft.d.ts +28 -11
- package/dist/room-draft.d.ts.map +1 -1
- package/dist/room-draft.js +185 -113
- package/dist/room-drafts.d.ts +24 -0
- package/dist/room-drafts.d.ts.map +1 -0
- package/dist/room-drafts.js +217 -0
- package/dist/rows.d.ts +31 -31
- package/dist/rows.d.ts.map +1 -1
- package/dist/rows.js +60 -125
- package/dist/runtime.d.ts +34 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +1 -0
- package/dist/save.d.ts +42 -50
- package/dist/save.d.ts.map +1 -1
- package/dist/save.js +571 -188
- package/dist/saves.d.ts +32 -0
- package/dist/saves.d.ts.map +1 -0
- package/dist/saves.js +413 -0
- package/dist/search.d.ts +12 -95
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +10 -266
- package/dist/seed.d.ts +11 -2
- package/dist/seed.d.ts.map +1 -1
- package/dist/seed.js +71 -1
- package/dist/shared.d.ts +4 -9
- package/dist/shared.d.ts.map +1 -1
- package/dist/shared.js +61 -120
- package/dist/shown.d.ts +32 -0
- package/dist/shown.d.ts.map +1 -0
- package/dist/shown.js +290 -0
- package/dist/socket/admission.d.ts +7 -0
- package/dist/socket/admission.d.ts.map +1 -0
- package/dist/socket/admission.js +39 -0
- package/dist/socket/answers.d.ts +6 -0
- package/dist/socket/answers.d.ts.map +1 -0
- package/dist/socket/answers.js +20 -0
- package/dist/socket/lanes.d.ts +20 -0
- package/dist/socket/lanes.d.ts.map +1 -0
- package/dist/socket/lanes.js +77 -0
- package/dist/socket/reads.d.ts +10 -0
- package/dist/socket/reads.d.ts.map +1 -0
- package/dist/socket/reads.js +31 -0
- package/dist/socket/session.d.ts +38 -0
- package/dist/socket/session.d.ts.map +1 -0
- package/dist/socket/session.js +23 -0
- package/dist/socket.d.ts +67 -0
- package/dist/socket.d.ts.map +1 -0
- package/dist/socket.js +170 -0
- package/dist/source.d.ts +61 -0
- package/dist/source.d.ts.map +1 -0
- package/dist/source.js +82 -0
- package/dist/status.d.ts +4 -5
- package/dist/status.d.ts.map +1 -1
- package/dist/status.js +3 -7
- package/dist/store/installs.d.ts +30 -0
- package/dist/store/installs.d.ts.map +1 -0
- package/dist/store/installs.js +34 -0
- package/dist/store/readiness.d.ts +10 -0
- package/dist/store/readiness.d.ts.map +1 -0
- package/dist/store/readiness.js +52 -0
- package/dist/store/records.d.ts +212 -0
- package/dist/store/records.d.ts.map +1 -0
- package/dist/store/records.js +430 -0
- package/dist/store/room-held.d.ts +8 -0
- package/dist/store/room-held.d.ts.map +1 -0
- package/dist/store/room-held.js +17 -0
- package/dist/store/space.d.ts +11 -0
- package/dist/store/space.d.ts.map +1 -0
- package/dist/store/space.js +41 -0
- package/dist/store/watchers.d.ts +17 -0
- package/dist/store/watchers.d.ts.map +1 -0
- package/dist/store/watchers.js +56 -0
- package/dist/store/writes.d.ts +24 -0
- package/dist/store/writes.d.ts.map +1 -0
- package/dist/store/writes.js +125 -0
- package/dist/store.d.ts +109 -166
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +153 -797
- package/dist/stored.d.ts +156 -0
- package/dist/stored.d.ts.map +1 -0
- package/dist/stored.js +649 -0
- package/dist/surface.d.ts +88 -0
- package/dist/surface.d.ts.map +1 -0
- package/dist/surface.js +223 -0
- package/dist/sync.d.ts +19 -0
- package/dist/sync.d.ts.map +1 -0
- package/dist/sync.js +521 -0
- package/dist/tables.d.ts +412 -0
- package/dist/tables.d.ts.map +1 -0
- package/dist/tables.js +737 -0
- package/dist/tallies.d.ts +18 -0
- package/dist/tallies.d.ts.map +1 -0
- package/dist/tallies.js +129 -0
- package/dist/transfer.d.ts +0 -5
- package/dist/transfer.d.ts.map +1 -1
- package/dist/turn.d.ts +1 -1
- package/dist/turn.d.ts.map +1 -1
- package/dist/turn.js +31 -20
- package/dist/validation.d.ts +29 -0
- package/dist/validation.d.ts.map +1 -0
- package/dist/validation.js +104 -0
- package/dist/windows.d.ts +1 -16
- package/dist/windows.d.ts.map +1 -1
- package/dist/windows.js +2 -79
- package/dist/wire.d.ts +72 -8
- package/dist/wire.d.ts.map +1 -1
- package/dist/wire.js +24 -5
- package/package.json +19 -19
- package/AGENTS.md +0 -299
- package/dist/chat/acts.d.ts +0 -33
- package/dist/chat/acts.d.ts.map +0 -1
- package/dist/chat/acts.js +0 -36
- package/dist/chat/context.d.ts +0 -3
- package/dist/chat/context.d.ts.map +0 -1
- package/dist/chat/earlier.d.ts +0 -10
- package/dist/chat/earlier.d.ts.map +0 -1
- package/dist/chat/earlier.js +0 -54
- package/dist/chat/input.d.ts +0 -9
- package/dist/chat/input.d.ts.map +0 -1
- package/dist/chat/input.js +0 -99
- package/dist/chat/kit.d.ts +0 -61
- package/dist/chat/kit.d.ts.map +0 -1
- package/dist/chat/kit.js +0 -123
- package/dist/chat/lasting.d.ts +0 -6
- package/dist/chat/lasting.d.ts.map +0 -1
- package/dist/chat/lasting.js +0 -19
- package/dist/chat/list.d.ts +0 -28
- package/dist/chat/list.d.ts.map +0 -1
- package/dist/chat/list.js +0 -103
- package/dist/chat/markdown.d.ts +0 -21
- package/dist/chat/markdown.d.ts.map +0 -1
- package/dist/chat/markdown.js +0 -246
- package/dist/chat/message.d.ts +0 -7
- package/dist/chat/message.d.ts.map +0 -1
- package/dist/chat/message.js +0 -202
- package/dist/chat/messages.d.ts +0 -24
- package/dist/chat/messages.d.ts.map +0 -1
- package/dist/chat/messages.js +0 -187
- package/dist/chat/name.d.ts +0 -6
- package/dist/chat/name.d.ts.map +0 -1
- package/dist/chat/name.js +0 -37
- package/dist/chat/opened.d.ts +0 -11
- package/dist/chat/opened.d.ts.map +0 -1
- package/dist/chat/opened.js +0 -17
- package/dist/chat/overlay.d.ts +0 -32
- package/dist/chat/overlay.d.ts.map +0 -1
- package/dist/chat/overlay.js +0 -107
- package/dist/chat/parts.d.ts +0 -57
- package/dist/chat/parts.d.ts.map +0 -1
- package/dist/chat/parts.js +0 -119
- package/dist/chat/pieces.d.ts +0 -62
- package/dist/chat/pieces.d.ts.map +0 -1
- package/dist/chat/pieces.js +0 -1110
- package/dist/chat/process.d.ts +0 -20
- package/dist/chat/process.d.ts.map +0 -1
- package/dist/chat/process.js +0 -32
- package/dist/chat/provider.d.ts +0 -64
- package/dist/chat/provider.d.ts.map +0 -1
- package/dist/chat/provider.js +0 -638
- package/dist/chat/question.d.ts +0 -9
- package/dist/chat/question.d.ts.map +0 -1
- package/dist/chat/question.js +0 -69
- package/dist/chat/questions.d.ts.map +0 -1
- package/dist/chat/read.d.ts.map +0 -1
- package/dist/chat/ready.d.ts +0 -34
- package/dist/chat/ready.d.ts.map +0 -1
- package/dist/chat/ready.js +0 -91
- package/dist/chat/steady.d.ts +0 -3
- package/dist/chat/steady.d.ts.map +0 -1
- package/dist/chat/steady.js +0 -26
- package/dist/chat/style.d.ts +0 -4
- package/dist/chat/style.d.ts.map +0 -1
- package/dist/chat/style.js +0 -302
- package/dist/chat.d.ts +0 -14
- package/dist/chat.d.ts.map +0 -1
- package/dist/chat.js +0 -8
- package/dist/fence.d.ts +0 -6
- package/dist/fence.d.ts.map +0 -1
- package/dist/fence.js +0 -10
- package/dist/read-side.d.ts +0 -27
- package/dist/read-side.d.ts.map +0 -1
- package/dist/read-side.js +0 -826
package/dist/save.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { changeBatchIdentity } from './identity.js';
|
|
1
|
+
import { apply, ARRAY_INDEX, ATTACHMENT, create, links, parse, pointerSegments, references, relative, relativize, retarget, roomReferences, serialize, target, } from '@agentshouse/mdmodel';
|
|
2
|
+
import { immutable, leaving, queuedFile, departure, } from './change.js';
|
|
3
|
+
import { collectionOf, definitionsAt, documentsOf, governing, } from './collection.js';
|
|
4
|
+
import { changeBatchIdentity, roomDraftRevision } from './identity.js';
|
|
6
5
|
import { overlaying } from './overlay.js';
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
6
|
+
import { ancestors } from './paths.js';
|
|
7
|
+
import { entriesIn, sorting } from './shared.js';
|
|
8
|
+
import { changedParts, changesOf, moveTarget, withdrawnDrafts, } from './room-draft.js';
|
|
9
|
+
import { paced, ROW_WEIGHT, stepped } from './turn.js';
|
|
10
|
+
import { validatedCollection } from './validation.js';
|
|
10
11
|
function refuse(code, file, location) {
|
|
11
12
|
return {
|
|
12
13
|
kind: 'refused',
|
|
@@ -19,9 +20,6 @@ function refuse(code, file, location) {
|
|
|
19
20
|
};
|
|
20
21
|
}
|
|
21
22
|
function repointed(document, definition, from, to, moves, settled) {
|
|
22
|
-
const parsed = parse(document.content, from);
|
|
23
|
-
if (!parsed.ok)
|
|
24
|
-
return [];
|
|
25
23
|
const changes = [];
|
|
26
24
|
const names = new Set();
|
|
27
25
|
for (const { pointer, value } of definition === undefined
|
|
@@ -44,13 +42,13 @@ function repointed(document, definition, from, to, moves, settled) {
|
|
|
44
42
|
}
|
|
45
43
|
if (changes.length === 0)
|
|
46
44
|
return [];
|
|
47
|
-
const applied = apply(
|
|
45
|
+
const applied = apply(document, changes, definition);
|
|
48
46
|
if (applied.outcome !== 'applied')
|
|
49
47
|
return [];
|
|
50
48
|
const next = applied.document;
|
|
51
49
|
return [...names].map((name) => ({ kind: 'field', path: from, name, value: next.fields[name] }));
|
|
52
50
|
}
|
|
53
|
-
function edited(document, changes) {
|
|
51
|
+
function edited(document, changes, definition) {
|
|
54
52
|
const parts = new Map();
|
|
55
53
|
for (const change of changes) {
|
|
56
54
|
if (change.kind === 'preamble')
|
|
@@ -65,7 +63,7 @@ function edited(document, changes) {
|
|
|
65
63
|
}
|
|
66
64
|
if (parts.size === 0)
|
|
67
65
|
return document;
|
|
68
|
-
const applied = apply(document, [...parts.values()]);
|
|
66
|
+
const applied = apply(document, [...parts.values()], definition);
|
|
69
67
|
return applied.outcome === 'applied' ? applied.document : document;
|
|
70
68
|
}
|
|
71
69
|
function bodies(path, before, after) {
|
|
@@ -80,7 +78,7 @@ function bodies(path, before, after) {
|
|
|
80
78
|
}
|
|
81
79
|
return changes;
|
|
82
80
|
}
|
|
83
|
-
function relinks(path, moved, before, moves) {
|
|
81
|
+
function relinks(path, moved, before, moves, definition) {
|
|
84
82
|
const changes = [];
|
|
85
83
|
for (const link of links(before, path)) {
|
|
86
84
|
const destination = moves.get(link.target) ?? link.target;
|
|
@@ -93,7 +91,7 @@ function relinks(path, moved, before, moves) {
|
|
|
93
91
|
}
|
|
94
92
|
if (changes.length === 0)
|
|
95
93
|
return { kind: 'changes', changes: [] };
|
|
96
|
-
const applied = apply(before, changes);
|
|
94
|
+
const applied = apply(before, changes, definition);
|
|
97
95
|
if (applied.outcome === 'refuse') {
|
|
98
96
|
return {
|
|
99
97
|
kind: 'refused',
|
|
@@ -116,6 +114,15 @@ function relinks(path, moved, before, moves) {
|
|
|
116
114
|
function partKey(change) {
|
|
117
115
|
return change.kind === 'section' ? `section ${change.name}` : change.kind;
|
|
118
116
|
}
|
|
117
|
+
const NAMES_A_SKIP_TESTS = 64;
|
|
118
|
+
const ENCODED = /[%&\\]/u;
|
|
119
|
+
function movedNames(moves) {
|
|
120
|
+
const names = new Set([...moves.keys()].map((path) => path.slice(path.lastIndexOf('/') + 1)));
|
|
121
|
+
return names.size > NAMES_A_SKIP_TESTS ? null : [...names];
|
|
122
|
+
}
|
|
123
|
+
function cannotLink(text, names) {
|
|
124
|
+
return names !== null && !ENCODED.test(text) && !names.some((name) => text.includes(name));
|
|
125
|
+
}
|
|
119
126
|
function* rewrites(changes, visible) {
|
|
120
127
|
const moves = new Map();
|
|
121
128
|
const settled = new Set();
|
|
@@ -141,16 +148,20 @@ function* rewrites(changes, visible) {
|
|
|
141
148
|
}
|
|
142
149
|
if (moves.size === 0)
|
|
143
150
|
return { kind: 'changes', changes };
|
|
151
|
+
const names = movedNames(moves);
|
|
144
152
|
const merged = [...changes];
|
|
145
153
|
const added = [];
|
|
146
|
-
for (const [path, document] of documentsOf(visible.collection)) {
|
|
154
|
+
for (const [path, document] of yield* documentsOf(visible.collection)) {
|
|
147
155
|
if (settled.has(path))
|
|
148
156
|
continue;
|
|
149
157
|
const moved = moves.get(path) ?? path;
|
|
150
|
-
const definition =
|
|
151
|
-
|
|
158
|
+
const definition = definitionsAt(visible.collection.composition, path)(document.type);
|
|
159
|
+
const repointing = repointed(document, definition, path, moved, moves, settled);
|
|
160
|
+
added.push(...repointing);
|
|
152
161
|
const own = parts.get(path) ?? [];
|
|
153
|
-
const relinked =
|
|
162
|
+
const relinked = moved === path && own.length === 0 && cannotLink(document.content, names)
|
|
163
|
+
? { kind: 'changes', changes: [] }
|
|
164
|
+
: relinks(path, moved, edited(document, own.map((index) => merged[index]), definition), moves, definition);
|
|
154
165
|
if (relinked.kind === 'refused')
|
|
155
166
|
return relinked;
|
|
156
167
|
const keyed = new Map();
|
|
@@ -175,7 +186,9 @@ function* rewrites(changes, visible) {
|
|
|
175
186
|
else
|
|
176
187
|
merged[index] = change;
|
|
177
188
|
}
|
|
178
|
-
|
|
189
|
+
const text = visible.collection.text[path].length;
|
|
190
|
+
const rewritten = repointing.length > 0 || relinked.changes.length > 0;
|
|
191
|
+
yield ROW_WEIGHT + text + (rewritten ? text : 0);
|
|
179
192
|
}
|
|
180
193
|
return {
|
|
181
194
|
kind: 'changes',
|
|
@@ -199,6 +212,71 @@ export function* providerRewritten(batch, visible) {
|
|
|
199
212
|
...(yield* anticipated(batch.changes, batch.preconditions, rewritten.changes, visible)),
|
|
200
213
|
};
|
|
201
214
|
}
|
|
215
|
+
function crossed(document, definition, path, to) {
|
|
216
|
+
const changes = [];
|
|
217
|
+
for (const link of links(document, path)) {
|
|
218
|
+
const written = to(link.target, link.href);
|
|
219
|
+
if (written !== undefined)
|
|
220
|
+
changes.push({ kind: 'link', href: link.href, to: written });
|
|
221
|
+
}
|
|
222
|
+
for (const { pointer, value } of definition === undefined
|
|
223
|
+
? []
|
|
224
|
+
: references(document, definition)) {
|
|
225
|
+
const named = target(path, value);
|
|
226
|
+
const written = named === null ? undefined : to(named, value);
|
|
227
|
+
if (written !== undefined) {
|
|
228
|
+
changes.push({ kind: 'reference', pointer, value: retarget(value, written), prior: value });
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return changes;
|
|
232
|
+
}
|
|
233
|
+
function rewritten(path, document, changes, definition) {
|
|
234
|
+
if (changes.length === 0)
|
|
235
|
+
return [];
|
|
236
|
+
const applied = apply(document, changes, definition);
|
|
237
|
+
if (applied.outcome !== 'applied')
|
|
238
|
+
return [];
|
|
239
|
+
const after = applied.document;
|
|
240
|
+
if (document.type === undefined)
|
|
241
|
+
return [{ kind: 'replace', path, content: serialize(after) }];
|
|
242
|
+
const names = new Set(changes.flatMap((change) => change.kind === 'reference' ? [pointerSegments(change.pointer)[0] ?? ''] : []));
|
|
243
|
+
return [
|
|
244
|
+
...[...names].map((name) => ({ kind: 'field', path, name, value: after.fields[name] })),
|
|
245
|
+
...bodies(path, document, after),
|
|
246
|
+
];
|
|
247
|
+
}
|
|
248
|
+
function roomPathOf(handle, path) {
|
|
249
|
+
return `/rooms/${handle}/${path}`;
|
|
250
|
+
}
|
|
251
|
+
function* leavingRewritten(batch, visible) {
|
|
252
|
+
const between = batch.between;
|
|
253
|
+
if (between === undefined)
|
|
254
|
+
return batch;
|
|
255
|
+
const moved = new Map(between.moves.map(({ path, to }) => [path, roomPathOf(between.destination.handle, to)]));
|
|
256
|
+
const derived = [];
|
|
257
|
+
for (const [path, document] of yield* documentsOf(visible.collection)) {
|
|
258
|
+
if (moved.has(path))
|
|
259
|
+
continue;
|
|
260
|
+
const definition = definitionsAt(visible.collection.composition, path)(document.type);
|
|
261
|
+
derived.push(...rewritten(path, document, crossed(document, definition, path, (named) => moved.get(named)), definition));
|
|
262
|
+
yield ROW_WEIGHT + visible.collection.text[path].length;
|
|
263
|
+
}
|
|
264
|
+
if (derived.length === 0)
|
|
265
|
+
return batch;
|
|
266
|
+
return {
|
|
267
|
+
...batch,
|
|
268
|
+
changes: [...derived, ...batch.changes],
|
|
269
|
+
preconditions: [...(yield* preconditionsOf(derived, visible, [])), ...batch.preconditions],
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
export function rewriting(batch, locator) {
|
|
273
|
+
return leaving(batch, locator) || batch.changes.some((change) => change.kind === 'move');
|
|
274
|
+
}
|
|
275
|
+
export function* effectiveIn(batch, locator, visible) {
|
|
276
|
+
return leaving(batch, locator)
|
|
277
|
+
? yield* leavingRewritten(batch, visible)
|
|
278
|
+
: yield* providerRewritten(batch, visible);
|
|
279
|
+
}
|
|
202
280
|
function* incoming(collection, removed, candidates) {
|
|
203
281
|
const found = new Map();
|
|
204
282
|
const note = (target, file) => {
|
|
@@ -209,14 +287,20 @@ function* incoming(collection, removed, candidates) {
|
|
|
209
287
|
found.set(target, referrers);
|
|
210
288
|
};
|
|
211
289
|
const read = function* (paths) {
|
|
212
|
-
|
|
290
|
+
const seen = new Set();
|
|
291
|
+
for (const path of paths) {
|
|
292
|
+
if (seen.has(path))
|
|
293
|
+
continue;
|
|
294
|
+
seen.add(path);
|
|
213
295
|
const document = collection.document(path);
|
|
214
296
|
if (document !== undefined)
|
|
215
297
|
yield [path, document];
|
|
216
298
|
}
|
|
217
299
|
};
|
|
218
|
-
for (const [path, document] of candidates === null
|
|
219
|
-
|
|
300
|
+
for (const [path, document] of candidates === null
|
|
301
|
+
? yield* documentsOf(collection)
|
|
302
|
+
: read(candidates)) {
|
|
303
|
+
const definition = definitionsAt(collection.composition, path)(document.type);
|
|
220
304
|
for (const { value, to } of definition === undefined ? [] : references(document, definition)) {
|
|
221
305
|
if (to.includes(ATTACHMENT))
|
|
222
306
|
continue;
|
|
@@ -226,7 +310,7 @@ function* incoming(collection, removed, candidates) {
|
|
|
226
310
|
}
|
|
227
311
|
yield ROW_WEIGHT + collection.text[path].length;
|
|
228
312
|
}
|
|
229
|
-
const [first] =
|
|
313
|
+
const [first] = yield* sorting(found.keys());
|
|
230
314
|
if (first === undefined)
|
|
231
315
|
return null;
|
|
232
316
|
const every = new Set();
|
|
@@ -239,14 +323,16 @@ function* incoming(collection, removed, candidates) {
|
|
|
239
323
|
code: 'referenced-missing-file',
|
|
240
324
|
retryable: false,
|
|
241
325
|
file: first,
|
|
242
|
-
referrers:
|
|
326
|
+
referrers: yield* sorting(every),
|
|
243
327
|
},
|
|
244
328
|
};
|
|
245
329
|
}
|
|
246
330
|
function affected(change) {
|
|
247
331
|
return change.kind === 'move' ? [change.path, change.to] : [change.path];
|
|
248
332
|
}
|
|
249
|
-
|
|
333
|
+
const WRITE = ['write'];
|
|
334
|
+
const MOVE_OUT = ['write', 'erase'];
|
|
335
|
+
function guard(changes, input, removing) {
|
|
250
336
|
const overridden = new Map();
|
|
251
337
|
const existing = {
|
|
252
338
|
has: (path) => overridden.get(path) ??
|
|
@@ -254,21 +340,17 @@ function guard(changes, input) {
|
|
|
254
340
|
add: (path) => void overridden.set(path, true),
|
|
255
341
|
delete: (path) => void overridden.set(path, false),
|
|
256
342
|
};
|
|
343
|
+
const occupied = (path) => input.folders.has(path) ? path : (ancestors(path).find((one) => existing.has(one)) ?? null);
|
|
257
344
|
for (const [index, change] of changes.entries()) {
|
|
258
345
|
for (const path of affected(change)) {
|
|
259
|
-
const
|
|
260
|
-
if (
|
|
261
|
-
return refuse(
|
|
262
|
-
if (covered(input.providerOwnedPaths, normalized)) {
|
|
263
|
-
return refuse('provider-owned-path', normalized);
|
|
264
|
-
}
|
|
265
|
-
const operation = change.kind === 'remove' ? 'delete' : 'write';
|
|
266
|
-
if (!input.local && !covered(input.operations[operation], normalized))
|
|
267
|
-
return refuse(`${operation}-grant`, normalized);
|
|
346
|
+
const refused = input.rule(path, change.kind === 'remove' ? removing : WRITE);
|
|
347
|
+
if (refused !== null)
|
|
348
|
+
return refuse(refused.code, refused.path);
|
|
268
349
|
}
|
|
269
|
-
if (change.kind === 'create') {
|
|
270
|
-
|
|
271
|
-
|
|
350
|
+
if (change.kind === 'create' || change.kind === 'arrival') {
|
|
351
|
+
const taken = existing.has(change.path) ? change.path : occupied(change.path);
|
|
352
|
+
if (taken !== null)
|
|
353
|
+
return refuse('path-collision', taken);
|
|
272
354
|
existing.add(change.path);
|
|
273
355
|
continue;
|
|
274
356
|
}
|
|
@@ -278,6 +360,9 @@ function guard(changes, input) {
|
|
|
278
360
|
input.bases.some((base) => base.kind === 'absent' && base.path === change.path))) {
|
|
279
361
|
return refuse('path-collision', change.path);
|
|
280
362
|
}
|
|
363
|
+
const taken = occupied(change.path);
|
|
364
|
+
if (taken !== null)
|
|
365
|
+
return refuse('path-collision', taken);
|
|
281
366
|
if (!input.local &&
|
|
282
367
|
existing.has(change.path) &&
|
|
283
368
|
input.visible.revisions[departure(changes, index)] === undefined) {
|
|
@@ -289,8 +374,9 @@ function guard(changes, input) {
|
|
|
289
374
|
if (change.kind === 'move') {
|
|
290
375
|
if (!existing.has(change.path))
|
|
291
376
|
return refuse('path-absent', change.path);
|
|
292
|
-
|
|
293
|
-
|
|
377
|
+
const taken = existing.has(change.to) ? change.to : occupied(change.to);
|
|
378
|
+
if (taken !== null)
|
|
379
|
+
return refuse('path-collision', taken);
|
|
294
380
|
if (unrevised(input, change.path)) {
|
|
295
381
|
return refuse('unknown-revision', change.path);
|
|
296
382
|
}
|
|
@@ -320,13 +406,13 @@ function guard(changes, input) {
|
|
|
320
406
|
return null;
|
|
321
407
|
}
|
|
322
408
|
function unrevised(input, path) {
|
|
323
|
-
return
|
|
409
|
+
return !input.local && !input.queued.has(path) && input.visible.revisions[path] === undefined;
|
|
324
410
|
}
|
|
325
|
-
|
|
411
|
+
function queuedDocuments(queue) {
|
|
326
412
|
const paths = new Set();
|
|
327
413
|
for (const batch of queue) {
|
|
328
414
|
for (const change of batch.changes) {
|
|
329
|
-
if (change.kind === 'binary')
|
|
415
|
+
if (change.kind === 'binary' || (change.kind === 'arrival' && !('content' in change)))
|
|
330
416
|
continue;
|
|
331
417
|
if (change.kind === 'remove')
|
|
332
418
|
paths.delete(change.path);
|
|
@@ -417,7 +503,10 @@ function depth(path) {
|
|
|
417
503
|
function* exceeding(limits, changes, effective, resulting) {
|
|
418
504
|
const written = effective.map((change) => (change.kind === 'move' ? change.to : change.path));
|
|
419
505
|
const created = changes
|
|
420
|
-
.filter((change) => change.kind === 'create' ||
|
|
506
|
+
.filter((change) => change.kind === 'create' ||
|
|
507
|
+
change.kind === 'move' ||
|
|
508
|
+
change.kind === 'binary' ||
|
|
509
|
+
change.kind === 'arrival')
|
|
421
510
|
.map((change) => (change.kind === 'move' ? change.to : change.path));
|
|
422
511
|
for (const limit of limits) {
|
|
423
512
|
const value = limit.value;
|
|
@@ -455,19 +544,16 @@ function* exceeding(limits, changes, effective, resulting) {
|
|
|
455
544
|
function shellRelative(text, path, handle, definition) {
|
|
456
545
|
if (!text.includes(`/rooms/${handle}/`))
|
|
457
546
|
return null;
|
|
458
|
-
const parsed = parse(text, path);
|
|
547
|
+
const parsed = parse(text, path, definition);
|
|
459
548
|
if (!parsed.ok)
|
|
460
549
|
return null;
|
|
461
550
|
const result = relativize(parsed.document, path, handle, definition(parsed.document.type));
|
|
462
551
|
return result.outcome === 'applied' && result.document.content !== text ? result.document : null;
|
|
463
552
|
}
|
|
464
553
|
function relativeField(path, name, value, handle, definition) {
|
|
465
|
-
|
|
466
|
-
if (definition === undefined ||
|
|
467
|
-
!opened.ok ||
|
|
468
|
-
!JSON.stringify(value ?? null).includes(`/rooms/${handle}/`))
|
|
554
|
+
if (definition === undefined || !JSON.stringify(value ?? null).includes(`/rooms/${handle}/`))
|
|
469
555
|
return value;
|
|
470
|
-
const held = apply(
|
|
556
|
+
const held = apply(create(definition.name, definition), [{ kind: 'field', name, value, prior: undefined }], definition);
|
|
471
557
|
if (held.outcome !== 'applied')
|
|
472
558
|
return value;
|
|
473
559
|
const result = relativize(held.document, path, handle, definition);
|
|
@@ -475,7 +561,7 @@ function relativeField(path, name, value, handle, definition) {
|
|
|
475
561
|
}
|
|
476
562
|
function relativeChanges(input) {
|
|
477
563
|
const { handle, composition, visible } = input;
|
|
478
|
-
const typed = (path) => (
|
|
564
|
+
const typed = (path) => definitionsAt(composition, path);
|
|
479
565
|
const body = (path, text) => shellRelative(text, path, handle, () => undefined)?.content ?? text;
|
|
480
566
|
return input.changes.map((change) => {
|
|
481
567
|
const definition = typed(change.path);
|
|
@@ -503,20 +589,8 @@ function relativeChanges(input) {
|
|
|
503
589
|
return { ...change, fields, sections };
|
|
504
590
|
});
|
|
505
591
|
}
|
|
506
|
-
function* unmodeledType(collection, composition) {
|
|
507
|
-
if (available(composition))
|
|
508
|
-
return false;
|
|
509
|
-
for (const [path, document] of documentsOf(collection)) {
|
|
510
|
-
if (document.type !== undefined &&
|
|
511
|
-
document.type !== 'any' &&
|
|
512
|
-
governing(composition, path).status === 'unavailable')
|
|
513
|
-
return true;
|
|
514
|
-
yield ROW_WEIGHT + collection.text[path].length;
|
|
515
|
-
}
|
|
516
|
-
return false;
|
|
517
|
-
}
|
|
518
592
|
function unqueueable(file) {
|
|
519
|
-
return
|
|
593
|
+
return refuse('unknown-revision', file);
|
|
520
594
|
}
|
|
521
595
|
function isPart(change) {
|
|
522
596
|
return change.kind === 'field' || change.kind === 'section' || change.kind === 'preamble';
|
|
@@ -524,8 +598,9 @@ function isPart(change) {
|
|
|
524
598
|
function touched(change) {
|
|
525
599
|
return change.kind === 'move' ? [change.path, change.to] : [change.path];
|
|
526
600
|
}
|
|
527
|
-
function partsApplied(content, path, changes) {
|
|
528
|
-
const
|
|
601
|
+
function partsApplied(content, path, changes, composition) {
|
|
602
|
+
const definitions = definitionsAt(composition, path);
|
|
603
|
+
const parsed = parse(content, path, definitions);
|
|
529
604
|
if (!parsed.ok)
|
|
530
605
|
return null;
|
|
531
606
|
const document = parsed.document;
|
|
@@ -548,12 +623,12 @@ function partsApplied(content, path, changes) {
|
|
|
548
623
|
else
|
|
549
624
|
parts.set('preamble', { kind: 'preamble', value: change.value, prior: document.preamble });
|
|
550
625
|
}
|
|
551
|
-
const applied = apply(document, [...parts.values()]);
|
|
626
|
+
const applied = apply(document, [...parts.values()], definitions(document.type));
|
|
552
627
|
return applied.outcome === 'applied' ? serialize(applied.document) : null;
|
|
553
628
|
}
|
|
554
|
-
function settledParts(changes, deferred) {
|
|
629
|
+
function settledParts(changes, deferred, composition) {
|
|
555
630
|
for (const [held, parts] of deferred) {
|
|
556
|
-
const content = partsApplied(held.content, held.path, parts);
|
|
631
|
+
const content = partsApplied(held.content, held.path, parts, composition);
|
|
557
632
|
if (content === null)
|
|
558
633
|
return held.path;
|
|
559
634
|
changes[changes.indexOf(held)] = { ...held, content };
|
|
@@ -617,8 +692,8 @@ function folded(changes, change, appending, deferred) {
|
|
|
617
692
|
changes.push(change);
|
|
618
693
|
return 'appended';
|
|
619
694
|
}
|
|
620
|
-
function relinked(batch, move, before, after, appending) {
|
|
621
|
-
const derived =
|
|
695
|
+
function* relinked(batch, move, before, after, appending) {
|
|
696
|
+
const derived = yield* rewrites([move], after);
|
|
622
697
|
if (derived.kind === 'refused')
|
|
623
698
|
return derived;
|
|
624
699
|
const changes = [...batch.changes];
|
|
@@ -628,7 +703,9 @@ function relinked(batch, move, before, after, appending) {
|
|
|
628
703
|
for (const change of derived.changes) {
|
|
629
704
|
if (change === move)
|
|
630
705
|
continue;
|
|
631
|
-
const unsettled = isPart(change)
|
|
706
|
+
const unsettled = isPart(change)
|
|
707
|
+
? null
|
|
708
|
+
: settledParts(changes, deferred, after.collection.composition);
|
|
632
709
|
if (unsettled !== null)
|
|
633
710
|
return unqueueable(unsettled);
|
|
634
711
|
const outcome = folded(changes, change, appending, deferred);
|
|
@@ -639,12 +716,13 @@ function relinked(batch, move, before, after, appending) {
|
|
|
639
716
|
if (outcome !== 'skipped')
|
|
640
717
|
changed = true;
|
|
641
718
|
}
|
|
642
|
-
const unsettled = settledParts(changes, deferred);
|
|
719
|
+
const unsettled = settledParts(changes, deferred, after.collection.composition);
|
|
643
720
|
if (unsettled !== null)
|
|
644
721
|
return unqueueable(unsettled);
|
|
645
722
|
if (!changed)
|
|
646
723
|
return { kind: 'relinked', batch };
|
|
647
724
|
const held = new Set(batch.preconditions.map(identity));
|
|
725
|
+
const added = yield* preconditionsOf(appended, before, []);
|
|
648
726
|
return {
|
|
649
727
|
kind: 'relinked',
|
|
650
728
|
batch: immutable({
|
|
@@ -653,7 +731,7 @@ function relinked(batch, move, before, after, appending) {
|
|
|
653
731
|
changes,
|
|
654
732
|
preconditions: [
|
|
655
733
|
...batch.preconditions,
|
|
656
|
-
...
|
|
734
|
+
...added.filter((precondition) => !held.has(identity(precondition))),
|
|
657
735
|
],
|
|
658
736
|
}),
|
|
659
737
|
};
|
|
@@ -699,11 +777,10 @@ function withdrawnFile(batch, change, at) {
|
|
|
699
777
|
];
|
|
700
778
|
return immutable({ ...batch, id: changeBatchIdentity(), changes, preconditions });
|
|
701
779
|
}
|
|
702
|
-
|
|
780
|
+
function* requeuing(queue, changes, visible, changeable, state) {
|
|
703
781
|
const next = [...queue];
|
|
704
782
|
const origins = new Map();
|
|
705
783
|
const withdrawn = new Map();
|
|
706
|
-
const owners = new Set();
|
|
707
784
|
const queued = new Set();
|
|
708
785
|
const kept = [];
|
|
709
786
|
const originOf = (batch) => origins.get(batch) ?? batch.id;
|
|
@@ -722,19 +799,19 @@ export function requeued(queue, changes, visible, changeable, state) {
|
|
|
722
799
|
return unqueueable(change.path);
|
|
723
800
|
const following = next.slice(index);
|
|
724
801
|
const rebuilt = [];
|
|
725
|
-
let before = change.kind === 'move' ? state(next.slice(0, index)) : visible;
|
|
802
|
+
let before = change.kind === 'move' ? yield* state(next.slice(0, index)) : visible;
|
|
726
803
|
for (const [offset, batch] of following.entries()) {
|
|
727
804
|
let current = batch;
|
|
728
805
|
if (change.kind === 'move') {
|
|
729
|
-
const after = state(next.slice(0, index + offset + 1));
|
|
806
|
+
const after = yield* state(next.slice(0, index + offset + 1));
|
|
730
807
|
const appending = offset === 0 && created(batch, queuedFile(batch, change.path, held.localId));
|
|
731
|
-
const relinking = relinked(batch, change, before, after, appending);
|
|
808
|
+
const relinking = yield* relinked(batch, change, before, after, appending);
|
|
732
809
|
if (relinking.kind === 'refused')
|
|
733
810
|
return relinking;
|
|
734
811
|
current = relinking.batch;
|
|
735
812
|
before = after;
|
|
736
813
|
}
|
|
737
|
-
if (current !== batch && !open(batch))
|
|
814
|
+
if (current !== batch && (!open(batch) || batch.between !== undefined))
|
|
738
815
|
return unqueueable(change.path);
|
|
739
816
|
rebuilt.push(offset === 0
|
|
740
817
|
? withdrawnFile(current, change, queuedFile(current, change.path, held.localId))
|
|
@@ -749,7 +826,6 @@ export function requeued(queue, changes, visible, changeable, state) {
|
|
|
749
826
|
if (replaced !== null)
|
|
750
827
|
origins.set(replaced, origin);
|
|
751
828
|
}
|
|
752
|
-
owners.add(originOf(owner));
|
|
753
829
|
next.splice(index, following.length, ...rebuilt.filter((batch) => batch !== null));
|
|
754
830
|
queued.add(change.path);
|
|
755
831
|
}
|
|
@@ -759,112 +835,68 @@ export function requeued(queue, changes, visible, changeable, state) {
|
|
|
759
835
|
changes: kept,
|
|
760
836
|
queued,
|
|
761
837
|
withdrawn,
|
|
762
|
-
owners,
|
|
763
838
|
};
|
|
764
839
|
}
|
|
765
|
-
function
|
|
766
|
-
return precondition.kind === 'field' ||
|
|
767
|
-
precondition.kind === 'section' ||
|
|
768
|
-
precondition.kind === 'preamble'
|
|
769
|
-
? identity(precondition)
|
|
770
|
-
: `path ${precondition.path}`;
|
|
771
|
-
}
|
|
772
|
-
export function joined(queue, owner, extra) {
|
|
773
|
-
const later = new Set(queue.slice(queue.indexOf(owner) + 1).flatMap((batch) => batch.changes.flatMap(touched)));
|
|
774
|
-
const clash = extra.changes.flatMap(touched).find((path) => later.has(path));
|
|
775
|
-
if (clash !== undefined)
|
|
776
|
-
return unqueueable(clash);
|
|
777
|
-
const changes = [...owner.changes];
|
|
778
|
-
const deferred = new Map();
|
|
779
|
-
for (const change of extra.changes) {
|
|
780
|
-
const unsettled = isPart(change) ? null : settledParts(changes, deferred);
|
|
781
|
-
if (unsettled !== null)
|
|
782
|
-
return unqueueable(unsettled);
|
|
783
|
-
if (folded(changes, change, true, deferred) === 'refused')
|
|
784
|
-
return unqueueable(change.path);
|
|
785
|
-
}
|
|
786
|
-
const unsettled = settledParts(changes, deferred);
|
|
787
|
-
if (unsettled !== null)
|
|
788
|
-
return unqueueable(unsettled);
|
|
789
|
-
const held = new Set(owner.preconditions.map(covering));
|
|
790
|
-
const written = new Set(owner.changes.flatMap((change) => change.kind === 'create' || change.kind === 'replace' ? [change.path] : []));
|
|
791
|
-
const preconditions = [
|
|
792
|
-
...owner.preconditions,
|
|
793
|
-
...extra.preconditions.filter((precondition) => {
|
|
794
|
-
const key = covering(precondition);
|
|
795
|
-
return (!held.has(key) && (key === `path ${precondition.path}` || !written.has(precondition.path)));
|
|
796
|
-
}),
|
|
797
|
-
];
|
|
798
|
-
const batch = immutable({
|
|
799
|
-
...owner,
|
|
800
|
-
...(extra.validation === undefined ? {} : { validation: extra.validation }),
|
|
801
|
-
id: changeBatchIdentity(),
|
|
802
|
-
changes,
|
|
803
|
-
preconditions,
|
|
804
|
-
});
|
|
805
|
-
return { kind: 'joined', batch, queue: queue.map((item) => (item === owner ? batch : item)) };
|
|
806
|
-
}
|
|
807
|
-
export function limitedIn(queue, batch, limits, state) {
|
|
840
|
+
function* limiting(queue, batch, limits, state) {
|
|
808
841
|
const at = queue.indexOf(batch);
|
|
809
|
-
const
|
|
810
|
-
|
|
842
|
+
const rewritten = yield* providerRewritten(batch, yield* state(queue.slice(0, at)));
|
|
843
|
+
const limited = yield* exceeding(limits, batch.changes, rewritten.changes, yield* state(queue.slice(0, at + 1)));
|
|
844
|
+
return limited?.refusal ?? null;
|
|
811
845
|
}
|
|
812
|
-
function
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
const
|
|
827
|
-
const
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
846
|
+
function typedText(content) {
|
|
847
|
+
if (content === undefined)
|
|
848
|
+
return false;
|
|
849
|
+
const parsed = parse(content);
|
|
850
|
+
return parsed.ok && parsed.document.type !== undefined && parsed.document.type !== 'any';
|
|
851
|
+
}
|
|
852
|
+
function touchedLibrary(resulting, touched, composition) {
|
|
853
|
+
const entries = touched
|
|
854
|
+
.flatMap((path) => {
|
|
855
|
+
const content = resulting.documents[path];
|
|
856
|
+
const binary = resulting.binaries[path] !== undefined;
|
|
857
|
+
return content === undefined && !binary ? [] : [{ path, content: content ?? null }];
|
|
858
|
+
})
|
|
859
|
+
.sort((left, right) => (left.path < right.path ? -1 : left.path > right.path ? 1 : 0));
|
|
860
|
+
const contents = new Map(entries.map(({ path, content }) => [path, content]));
|
|
861
|
+
const binaries = new Set(Array.from(entriesIn(resulting.binaries), ([path]) => path));
|
|
862
|
+
return validatedCollection(() => Promise.resolve({ entries, next: null }), async (paths) => {
|
|
863
|
+
const known = new Map();
|
|
864
|
+
const pace = paced();
|
|
865
|
+
for (const path of paths) {
|
|
866
|
+
const content = contents.get(path);
|
|
867
|
+
if (binaries.has(path))
|
|
868
|
+
known.set(path, { type: undefined, binary: true });
|
|
869
|
+
else if (content !== undefined && content !== null) {
|
|
870
|
+
const read = parse(content, path, definitionsAt(composition, path));
|
|
871
|
+
if (read.ok)
|
|
872
|
+
known.set(path, { type: read.document.type ?? 'any', binary: false });
|
|
873
|
+
await pace(content.length + ROW_WEIGHT);
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
return known;
|
|
877
|
+
}, composition);
|
|
878
|
+
}
|
|
879
|
+
async function judging(visible, resulting, composition, validate, removed, touched) {
|
|
833
880
|
if (removed.size > 0) {
|
|
834
|
-
const
|
|
835
|
-
const referenced = yield* incoming(collectionOf(resulting.documents, input.composition), removed, clean === null || referrers === null
|
|
836
|
-
? null
|
|
837
|
-
: [...referrers, ...(yield* differences(clean.text, resulting.documents))]);
|
|
881
|
+
const referenced = await stepped(incoming(collectionOf(resulting.documents, composition), removed, null));
|
|
838
882
|
if (referenced !== null)
|
|
839
883
|
return referenced;
|
|
840
884
|
}
|
|
841
|
-
const typed = (content) => {
|
|
842
|
-
if (content === undefined)
|
|
843
|
-
return false;
|
|
844
|
-
const parsed = parse(content);
|
|
845
|
-
return parsed.ok && parsed.document.type !== undefined && parsed.document.type !== 'any';
|
|
846
|
-
};
|
|
847
|
-
const touched = [...new Set(predicted.changes.flatMap(affected))];
|
|
848
885
|
const typedPaths = [];
|
|
886
|
+
const pace = paced();
|
|
849
887
|
for (const path of touched) {
|
|
850
|
-
const before =
|
|
888
|
+
const before = visible.documents[path];
|
|
851
889
|
const after = resulting.documents[path];
|
|
852
|
-
if (
|
|
890
|
+
if (typedText(before) || typedText(after))
|
|
853
891
|
typedPaths.push(path);
|
|
854
|
-
|
|
892
|
+
await pace((before?.length ?? 0) + (after?.length ?? 0) + ROW_WEIGHT);
|
|
855
893
|
}
|
|
856
|
-
const
|
|
857
|
-
const withoutModel = typedPaths.find((path) => governing(input.composition, path).status === 'unavailable');
|
|
894
|
+
const withoutModel = typedPaths.find((path) => governing(composition, path).status === 'unavailable');
|
|
858
895
|
if (withoutModel !== undefined)
|
|
859
896
|
return refuse('model-unavailable', withoutModel);
|
|
860
|
-
const verdict =
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
: yield* validateCollection(needsModel
|
|
864
|
-
? resulting.documents
|
|
865
|
-
: Object.fromEntries(touched.flatMap((path) => resulting.documents[path] === undefined
|
|
866
|
-
? []
|
|
867
|
-
: [[path, resulting.documents[path]]])), Object.keys(resulting.binaries), input.composition);
|
|
897
|
+
const verdict = typedPaths.length > 0
|
|
898
|
+
? await validate(resulting, touched)
|
|
899
|
+
: await touchedLibrary(resulting, touched, composition);
|
|
868
900
|
if (verdict.verdict === 'refuse') {
|
|
869
901
|
return {
|
|
870
902
|
kind: 'refused',
|
|
@@ -877,12 +909,93 @@ function* batching(input) {
|
|
|
877
909
|
},
|
|
878
910
|
};
|
|
879
911
|
}
|
|
880
|
-
|
|
912
|
+
return {
|
|
913
|
+
kind: 'judged',
|
|
914
|
+
validated: typedPaths
|
|
915
|
+
.map((path) => governing(composition, path))
|
|
916
|
+
.find((model) => model.status !== 'unavailable'),
|
|
917
|
+
};
|
|
918
|
+
}
|
|
919
|
+
function placed(reference) {
|
|
920
|
+
const key = roomPathOf(reference.handle, reference.path);
|
|
921
|
+
if (reference.kind === 'link')
|
|
922
|
+
return `link ${key}`;
|
|
923
|
+
if (reference.kind === 'item')
|
|
924
|
+
return `section ${reference.section} ${key}`;
|
|
925
|
+
const pointer = reference.pointer
|
|
926
|
+
.split('/')
|
|
927
|
+
.map((segment) => (ARRAY_INDEX.test(segment) ? '-' : segment))
|
|
928
|
+
.join('/');
|
|
929
|
+
return `field ${pointer} ${key}`;
|
|
930
|
+
}
|
|
931
|
+
function roomReferencesIn(path, content, composition) {
|
|
932
|
+
if (content === undefined)
|
|
933
|
+
return [];
|
|
934
|
+
const definitions = definitionsAt(composition, path);
|
|
935
|
+
const parsed = parse(content, path, definitions);
|
|
936
|
+
if (!parsed.ok)
|
|
937
|
+
return [];
|
|
938
|
+
return roomReferences(parsed.document, definitions(parsed.document.type));
|
|
939
|
+
}
|
|
940
|
+
function* admittedRooms(changes, visible, resulting, composition) {
|
|
941
|
+
const from = new Map();
|
|
942
|
+
for (const [index, change] of changes.entries()) {
|
|
943
|
+
if (change.kind === 'move')
|
|
944
|
+
from.set(change.to, departure(changes, index));
|
|
945
|
+
}
|
|
946
|
+
for (const path of [...new Set(changes.flatMap(affected))].sort()) {
|
|
947
|
+
const now = resulting.documents[path];
|
|
948
|
+
if (now === undefined)
|
|
949
|
+
continue;
|
|
950
|
+
const held = new Set(roomReferencesIn(path, visible.documents[from.get(path) ?? path], composition).map(placed));
|
|
951
|
+
for (const reference of roomReferencesIn(path, now, composition)) {
|
|
952
|
+
if (reference.kind === 'link' || reference.crossRoom === true)
|
|
953
|
+
continue;
|
|
954
|
+
if (held.has(placed(reference)))
|
|
955
|
+
continue;
|
|
956
|
+
return refuse('unresolved-reference', path, reference.kind === 'field' ? reference.pointer : reference.section);
|
|
957
|
+
}
|
|
958
|
+
yield ROW_WEIGHT + now.length;
|
|
959
|
+
}
|
|
960
|
+
return null;
|
|
961
|
+
}
|
|
962
|
+
function validationOf(validated) {
|
|
963
|
+
return validated === undefined
|
|
964
|
+
? {}
|
|
965
|
+
: {
|
|
966
|
+
validation: {
|
|
967
|
+
identity: validated.snapshot.identity,
|
|
968
|
+
version: validated.snapshot.version,
|
|
969
|
+
source: validated.status,
|
|
970
|
+
},
|
|
971
|
+
};
|
|
972
|
+
}
|
|
973
|
+
async function batching(input) {
|
|
974
|
+
const changes = relativeChanges(input);
|
|
975
|
+
const structural = changes.find((change) => change.kind === 'move' || change.kind === 'remove');
|
|
976
|
+
if (structural !== undefined && input.unmodeled) {
|
|
977
|
+
return refuse('model-unavailable', structural.path);
|
|
978
|
+
}
|
|
979
|
+
if (changes.length === 0)
|
|
980
|
+
return refuse('no-change');
|
|
981
|
+
const rewritten = await stepped(rewrites(changes, input.visible));
|
|
982
|
+
if (rewritten.kind === 'refused')
|
|
983
|
+
return rewritten;
|
|
984
|
+
const refusal = guard(changes, input, WRITE);
|
|
985
|
+
if (refusal !== null)
|
|
986
|
+
return refusal;
|
|
987
|
+
const preconditions = await stepped(preconditionsOf(changes, input.visible, input.bases));
|
|
988
|
+
const predicted = await stepped(anticipated(changes, preconditions, rewritten.changes, input.visible));
|
|
989
|
+
const resulting = await stepped(overlaying(input.visible, [{ id: '', baseRoomVersion: input.baseRoomVersion, ...predicted }], input.composition));
|
|
990
|
+
const judged = await judging(input.visible, resulting, input.composition, input.validate, new Set(changes.filter((change) => change.kind === 'remove').map((change) => change.path)), [...new Set(predicted.changes.flatMap(affected))]);
|
|
991
|
+
if (judged.kind === 'refused')
|
|
992
|
+
return judged;
|
|
993
|
+
const crossing = await stepped(admittedRooms(changes, input.visible, resulting, input.composition));
|
|
994
|
+
if (crossing !== null)
|
|
995
|
+
return crossing;
|
|
996
|
+
const limited = await stepped(exceeding(input.providerLimits, changes, predicted.changes, resulting));
|
|
881
997
|
if (limited !== null)
|
|
882
998
|
return limited;
|
|
883
|
-
const validated = typedPaths
|
|
884
|
-
.map((path) => governing(input.composition, path))
|
|
885
|
-
.find((model) => model.status !== 'unavailable');
|
|
886
999
|
return {
|
|
887
1000
|
kind: 'batch',
|
|
888
1001
|
batch: immutable({
|
|
@@ -891,18 +1004,288 @@ function* batching(input) {
|
|
|
891
1004
|
changes,
|
|
892
1005
|
preconditions,
|
|
893
1006
|
...(input.writtenBy === undefined ? {} : { writtenBy: input.writtenBy }),
|
|
894
|
-
...(validated
|
|
895
|
-
? {}
|
|
896
|
-
: {
|
|
897
|
-
validation: {
|
|
898
|
-
identity: validated.snapshot.identity,
|
|
899
|
-
version: validated.snapshot.version,
|
|
900
|
-
source: validated.status,
|
|
901
|
-
},
|
|
902
|
-
}),
|
|
1007
|
+
...validationOf(judged.validated),
|
|
903
1008
|
}),
|
|
904
1009
|
};
|
|
905
1010
|
}
|
|
906
|
-
|
|
907
|
-
|
|
1011
|
+
const MOVE_DOCUMENTS = 'move-documents';
|
|
1012
|
+
function heldAt(visible, path) {
|
|
1013
|
+
return visible.documents[path] !== undefined || visible.binaries[path] !== undefined;
|
|
1014
|
+
}
|
|
1015
|
+
function arrivingText(path, to, source, moved) {
|
|
1016
|
+
const content = source.visible.documents[path];
|
|
1017
|
+
const document = source.visible.collection.document(path);
|
|
1018
|
+
if (document === undefined)
|
|
1019
|
+
return content;
|
|
1020
|
+
const definition = definitionsAt(source.composition, path)(document.type);
|
|
1021
|
+
const changes = crossed(document, definition, path, (named, href) => {
|
|
1022
|
+
const at = moved.get(named);
|
|
1023
|
+
if (at !== undefined)
|
|
1024
|
+
return target(to, href) === at ? undefined : relative(to, at);
|
|
1025
|
+
return heldAt(source.visible, named) ? roomPathOf(source.handle, named) : undefined;
|
|
1026
|
+
});
|
|
1027
|
+
if (changes.length === 0)
|
|
1028
|
+
return content;
|
|
1029
|
+
const applied = apply(document, changes, definition);
|
|
1030
|
+
return applied.outcome === 'applied' ? serialize(applied.document) : content;
|
|
1031
|
+
}
|
|
1032
|
+
function arrivalOf({ path, to }, source, moved) {
|
|
1033
|
+
const { documents, revisions, fileIds, created, changes, binaries } = source.visible;
|
|
1034
|
+
const kept = {
|
|
1035
|
+
...(fileIds[path] === undefined ? {} : { fileId: fileIds[path] }),
|
|
1036
|
+
...(created[path] === undefined ? {} : { created: created[path] }),
|
|
1037
|
+
...(changes[path] === undefined ? {} : { change: changes[path] }),
|
|
1038
|
+
};
|
|
1039
|
+
const heads = {
|
|
1040
|
+
...(revisions[path] === undefined ? {} : { revision: revisions[path] }),
|
|
1041
|
+
...kept,
|
|
1042
|
+
};
|
|
1043
|
+
const binary = binaries[path];
|
|
1044
|
+
if (binary?.state === 'accepted') {
|
|
1045
|
+
const { byteLength, mediaType, sha256 } = binary;
|
|
1046
|
+
return {
|
|
1047
|
+
kind: 'arrival',
|
|
1048
|
+
path: to,
|
|
1049
|
+
binary: { byteLength, mediaType, sha256 },
|
|
1050
|
+
...heads,
|
|
1051
|
+
};
|
|
1052
|
+
}
|
|
1053
|
+
const content = arrivingText(path, to, source, moved);
|
|
1054
|
+
return {
|
|
1055
|
+
kind: 'arrival',
|
|
1056
|
+
path: to,
|
|
1057
|
+
content,
|
|
1058
|
+
...(content === documents[path] ? heads : kept),
|
|
1059
|
+
};
|
|
1060
|
+
}
|
|
1061
|
+
async function moving(input) {
|
|
1062
|
+
const { source, destination, moves } = input;
|
|
1063
|
+
const removals = moves.map(({ path }) => ({ kind: 'remove', path }));
|
|
1064
|
+
const leaves = guard(removals, { ...source, local: input.local, bases: [] }, MOVE_OUT);
|
|
1065
|
+
if (leaves !== null)
|
|
1066
|
+
return leaves;
|
|
1067
|
+
const uploading = moves.find(({ path }) => source.visible.binaries[path]?.state === 'pending');
|
|
1068
|
+
if (uploading !== undefined)
|
|
1069
|
+
return refuse('unknown-revision', uploading.path);
|
|
1070
|
+
const moved = new Map(moves.map(({ path, to }) => [path, to]));
|
|
1071
|
+
const arrivals = moves.map((move) => arrivalOf(move, source, moved));
|
|
1072
|
+
const arrives = guard(arrivals, { ...destination, local: input.local, bases: [] }, WRITE);
|
|
1073
|
+
if (arrives !== null)
|
|
1074
|
+
return arrives;
|
|
1075
|
+
const bound = input.providerLimits.find((limit) => limit.name === MOVE_DOCUMENTS)?.value;
|
|
1076
|
+
if (typeof bound === 'number' && moves.length > bound) {
|
|
1077
|
+
return refuse('provider-limit', undefined, MOVE_DOCUMENTS);
|
|
1078
|
+
}
|
|
1079
|
+
if (source.unmodeled)
|
|
1080
|
+
return refuse('model-unavailable', moves[0].path);
|
|
1081
|
+
const id = changeBatchIdentity();
|
|
1082
|
+
const between = {
|
|
1083
|
+
source: { room: source.room, baseRoomVersion: source.baseRoomVersion },
|
|
1084
|
+
destination: {
|
|
1085
|
+
room: destination.room,
|
|
1086
|
+
baseRoomVersion: destination.baseRoomVersion,
|
|
1087
|
+
handle: destination.handle,
|
|
1088
|
+
},
|
|
1089
|
+
moves,
|
|
1090
|
+
};
|
|
1091
|
+
const written = input.writtenBy === undefined ? {} : { writtenBy: input.writtenBy };
|
|
1092
|
+
const leavingBatch = {
|
|
1093
|
+
id,
|
|
1094
|
+
baseRoomVersion: source.baseRoomVersion,
|
|
1095
|
+
changes: removals,
|
|
1096
|
+
preconditions: moves.map(({ path }) => existence(source.visible, path)),
|
|
1097
|
+
...written,
|
|
1098
|
+
between,
|
|
1099
|
+
};
|
|
1100
|
+
const effective = await stepped(leavingRewritten(leavingBatch, source.visible));
|
|
1101
|
+
const left = await stepped(overlaying(source.visible, [effective], source.composition));
|
|
1102
|
+
const sourceJudged = await judging(source.visible, left, source.composition, source.validate, new Set(moves.map(({ path }) => path)), [...new Set(effective.changes.map((change) => change.path))]);
|
|
1103
|
+
if (sourceJudged.kind === 'refused')
|
|
1104
|
+
return sourceJudged;
|
|
1105
|
+
const arrivingBatch = {
|
|
1106
|
+
id,
|
|
1107
|
+
baseRoomVersion: destination.baseRoomVersion,
|
|
1108
|
+
changes: arrivals,
|
|
1109
|
+
preconditions: moves.map(({ to }) => ({ kind: 'absent', path: to })),
|
|
1110
|
+
...written,
|
|
1111
|
+
between,
|
|
1112
|
+
};
|
|
1113
|
+
const arrived = await stepped(overlaying(destination.visible, [arrivingBatch], destination.composition));
|
|
1114
|
+
const destinationJudged = await judging(destination.visible, arrived, destination.composition, destination.validate, new Set(), moves.map(({ to }) => to));
|
|
1115
|
+
if (destinationJudged.kind === 'refused')
|
|
1116
|
+
return destinationJudged;
|
|
1117
|
+
const limited = (await stepped(exceeding(input.providerLimits, removals, effective.changes, left))) ??
|
|
1118
|
+
(await stepped(exceeding(input.providerLimits, arrivals, arrivals, arrived)));
|
|
1119
|
+
if (limited !== null)
|
|
1120
|
+
return limited;
|
|
1121
|
+
return { kind: 'moved', leaving: immutable(leavingBatch), arriving: immutable(arrivingBatch) };
|
|
1122
|
+
}
|
|
1123
|
+
function changedUnderneath(saved, visible) {
|
|
1124
|
+
const parts = saved.flatMap(({ path, parts: held }) => changedParts(path, held, visible));
|
|
1125
|
+
return parts.length === 0
|
|
1126
|
+
? null
|
|
1127
|
+
: { kind: 'refused', refusal: { code: 'changed-underneath', retryable: false, parts } };
|
|
1128
|
+
}
|
|
1129
|
+
function sideOf(room, visible) {
|
|
1130
|
+
const { record } = room;
|
|
1131
|
+
return {
|
|
1132
|
+
room: record.locator,
|
|
1133
|
+
handle: record.declaration.handle,
|
|
1134
|
+
baseRoomVersion: record.roomVersion,
|
|
1135
|
+
composition: room.composition,
|
|
1136
|
+
rule: room.rule,
|
|
1137
|
+
visible,
|
|
1138
|
+
folders: room.folders,
|
|
1139
|
+
queued: queuedDocuments(room.queue),
|
|
1140
|
+
unmodeled: !room.modelled,
|
|
1141
|
+
validate: room.validate,
|
|
1142
|
+
};
|
|
1143
|
+
}
|
|
1144
|
+
function carriedDrafts(ask, from, into) {
|
|
1145
|
+
return new Map(ask.saved.flatMap(({ path, parts }) => {
|
|
1146
|
+
const record = from.drafts.get(path);
|
|
1147
|
+
if (parts.move === undefined || record === undefined || ask.consumed.has(path))
|
|
1148
|
+
return [];
|
|
1149
|
+
const to = moveTarget(parts.move);
|
|
1150
|
+
if (into.drafts.has(to))
|
|
1151
|
+
return [];
|
|
1152
|
+
const locator = into.record.locator;
|
|
1153
|
+
return [[path, { ...record, locator, path: to, revision: roomDraftRevision() }]];
|
|
1154
|
+
}));
|
|
1155
|
+
}
|
|
1156
|
+
async function plannedMove(ask, source, into) {
|
|
1157
|
+
const destination = into.record.locator;
|
|
1158
|
+
const mixed = ask.saved.find(({ parts }) => Object.keys(parts).length !== 1 || parts.move?.room !== destination);
|
|
1159
|
+
if (mixed !== undefined)
|
|
1160
|
+
return refuse('mixed-room-move', mixed.path);
|
|
1161
|
+
const leavingVisible = await stepped(source.replay(source.queue));
|
|
1162
|
+
const changed = changedUnderneath(ask.saved, leavingVisible);
|
|
1163
|
+
if (changed !== null)
|
|
1164
|
+
return changed;
|
|
1165
|
+
const moves = ask.saved.map(({ path, parts }) => ({ path, to: moveTarget(parts.move) }));
|
|
1166
|
+
const built = await moving({
|
|
1167
|
+
source: sideOf(source, leavingVisible),
|
|
1168
|
+
destination: sideOf(into, await stepped(into.replay(into.queue))),
|
|
1169
|
+
moves,
|
|
1170
|
+
providerLimits: ask.providerLimits,
|
|
1171
|
+
local: ask.local,
|
|
1172
|
+
...(ask.writtenBy === undefined ? {} : { writtenBy: ask.writtenBy }),
|
|
1173
|
+
});
|
|
1174
|
+
if (built.kind === 'refused')
|
|
1175
|
+
return built;
|
|
1176
|
+
const { id } = built.leaving;
|
|
1177
|
+
const carried = carriedDrafts(ask, source, into);
|
|
1178
|
+
return {
|
|
1179
|
+
kind: 'planned',
|
|
1180
|
+
identifier: id,
|
|
1181
|
+
rooms: [
|
|
1182
|
+
{
|
|
1183
|
+
locator: source.record.locator,
|
|
1184
|
+
queue: [...source.queue, built.leaving],
|
|
1185
|
+
saved: new Map([[id, ask.saved]]),
|
|
1186
|
+
consumed: [...ask.consumed, ...carried.keys()],
|
|
1187
|
+
carried: [],
|
|
1188
|
+
},
|
|
1189
|
+
{
|
|
1190
|
+
locator: destination,
|
|
1191
|
+
queue: [...into.queue, built.arriving],
|
|
1192
|
+
saved: new Map([
|
|
1193
|
+
[id, [...carried.values()].map(({ path }) => ({ path, parts: {} }))],
|
|
1194
|
+
]),
|
|
1195
|
+
consumed: [],
|
|
1196
|
+
carried: [...carried.values()],
|
|
1197
|
+
},
|
|
1198
|
+
],
|
|
1199
|
+
};
|
|
1200
|
+
}
|
|
1201
|
+
async function plannedWithin(ask, room) {
|
|
1202
|
+
const { record } = room;
|
|
1203
|
+
const visible = await stepped(room.replay(room.queue));
|
|
1204
|
+
const changed = changedUnderneath(ask.saved, visible);
|
|
1205
|
+
if (changed !== null)
|
|
1206
|
+
return changed;
|
|
1207
|
+
const parts = ask.saved.map(({ path, parts: held }) => changesOf(path, held));
|
|
1208
|
+
const changes = parts.flatMap(({ changes: made }) => made);
|
|
1209
|
+
const requeue = await stepped(requeuing(room.queue, changes, visible, (batch) => room.changeable.has(batch.id), room.replay));
|
|
1210
|
+
if (requeue.kind === 'refused')
|
|
1211
|
+
return requeue;
|
|
1212
|
+
const foreign = [...requeue.withdrawn.keys()].some((origin) => {
|
|
1213
|
+
const writer = room.queue.find((batch) => batch.id === origin)?.writtenBy;
|
|
1214
|
+
return writer?.app !== ask.writtenBy?.app || writer?.release !== ask.writtenBy?.release;
|
|
1215
|
+
});
|
|
1216
|
+
if (foreign)
|
|
1217
|
+
return unqueueable([...requeue.queued].at(-1));
|
|
1218
|
+
const input = {
|
|
1219
|
+
local: ask.local,
|
|
1220
|
+
baseRoomVersion: record.roomVersion,
|
|
1221
|
+
composition: room.composition,
|
|
1222
|
+
rule: room.rule,
|
|
1223
|
+
providerLimits: ask.providerLimits,
|
|
1224
|
+
handle: record.declaration.handle,
|
|
1225
|
+
visible,
|
|
1226
|
+
folders: room.folders,
|
|
1227
|
+
changes,
|
|
1228
|
+
bases: parts.flatMap(({ bases }) => bases),
|
|
1229
|
+
queued: queuedDocuments(room.queue),
|
|
1230
|
+
writtenBy: ask.writtenBy,
|
|
1231
|
+
unmodeled: !room.modelled,
|
|
1232
|
+
validate: room.validate,
|
|
1233
|
+
};
|
|
1234
|
+
const rewritten = requeue.withdrawn.size > 0;
|
|
1235
|
+
if (rewritten) {
|
|
1236
|
+
const whole = await batching({
|
|
1237
|
+
...input,
|
|
1238
|
+
providerLimits: [],
|
|
1239
|
+
queued: new Set([...input.queued, ...requeue.queued]),
|
|
1240
|
+
});
|
|
1241
|
+
if (whole.kind === 'refused')
|
|
1242
|
+
return whole;
|
|
1243
|
+
}
|
|
1244
|
+
const own = !rewritten
|
|
1245
|
+
? await batching(input)
|
|
1246
|
+
: requeue.changes.length === 0
|
|
1247
|
+
? null
|
|
1248
|
+
: await batching({
|
|
1249
|
+
...input,
|
|
1250
|
+
visible: await stepped(room.replay(requeue.queue)),
|
|
1251
|
+
changes: requeue.changes,
|
|
1252
|
+
queued: queuedDocuments(requeue.queue),
|
|
1253
|
+
});
|
|
1254
|
+
if (own?.kind === 'refused')
|
|
1255
|
+
return own;
|
|
1256
|
+
const queue = own === null ? requeue.queue : [...requeue.queue, own.batch];
|
|
1257
|
+
const saved = new Map();
|
|
1258
|
+
for (const [origin, rebuilt] of requeue.withdrawn) {
|
|
1259
|
+
if (rebuilt === null)
|
|
1260
|
+
continue;
|
|
1261
|
+
const limited = await stepped(limiting(queue, rebuilt, ask.providerLimits, room.replay));
|
|
1262
|
+
if (limited !== null)
|
|
1263
|
+
return { kind: 'refused', refusal: limited };
|
|
1264
|
+
saved.set(rebuilt.id, withdrawnDrafts(room.saved.get(origin) ?? [], changes));
|
|
1265
|
+
}
|
|
1266
|
+
if (own !== null) {
|
|
1267
|
+
saved.set(own.batch.id, ask.saved.filter(({ path }) => !requeue.queued.has(path)));
|
|
1268
|
+
}
|
|
1269
|
+
const carried = carriedDrafts(ask, room, room);
|
|
1270
|
+
return {
|
|
1271
|
+
kind: 'planned',
|
|
1272
|
+
identifier: own?.batch.id ??
|
|
1273
|
+
[...requeue.withdrawn].map(([origin, rebuilt]) => rebuilt?.id ?? origin).at(-1),
|
|
1274
|
+
rooms: [
|
|
1275
|
+
{
|
|
1276
|
+
locator: record.locator,
|
|
1277
|
+
queue,
|
|
1278
|
+
saved,
|
|
1279
|
+
consumed: [...ask.consumed, ...carried.keys()],
|
|
1280
|
+
carried: [...carried.values()],
|
|
1281
|
+
},
|
|
1282
|
+
],
|
|
1283
|
+
};
|
|
1284
|
+
}
|
|
1285
|
+
export function destination(locator, saved) {
|
|
1286
|
+
const into = saved.find(({ parts }) => parts.move?.room !== undefined && parts.move.room !== locator);
|
|
1287
|
+
return into?.parts.move?.room ?? null;
|
|
1288
|
+
}
|
|
1289
|
+
export function planSave(ask, room, into) {
|
|
1290
|
+
return into === undefined ? plannedWithin(ask, room) : plannedMove(ask, room, into);
|
|
908
1291
|
}
|