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