@agentshouse/mdruntime 0.1.0-c99e5271060cd → 0.1.0-ca10e63188fc5
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 +21 -8
- package/dist/adapter.d.ts +134 -30
- package/dist/adapter.d.ts.map +1 -1
- package/dist/attached-model.d.ts +5 -1
- package/dist/attached-model.d.ts.map +1 -1
- package/dist/attached-model.js +21 -4
- package/dist/attempt.d.ts +16 -0
- package/dist/attempt.d.ts.map +1 -0
- package/dist/attempt.js +70 -0
- package/dist/binary.d.ts +18 -0
- package/dist/binary.d.ts.map +1 -0
- package/dist/binary.js +8 -0
- package/dist/bootstrap.d.ts +32 -9
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +192 -67
- package/dist/chain.d.ts +11 -0
- package/dist/chain.d.ts.map +1 -0
- package/dist/chain.js +64 -0
- package/dist/change-set.d.ts +9 -14
- package/dist/change-set.d.ts.map +1 -1
- package/dist/change-set.js +11 -62
- package/dist/change.d.ts +41 -0
- package/dist/change.d.ts.map +1 -1
- package/dist/change.js +34 -0
- package/dist/client.d.ts +31 -12
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +104 -7
- package/dist/collection.d.ts +12 -6
- package/dist/collection.d.ts.map +1 -1
- package/dist/collection.js +51 -76
- package/dist/compile.d.ts +16 -0
- package/dist/compile.d.ts.map +1 -0
- package/dist/compile.js +765 -0
- package/dist/connection.d.ts +52 -16
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +3656 -766
- package/dist/conversation.d.ts +37 -11
- package/dist/conversation.d.ts.map +1 -1
- package/dist/conversation.js +576 -179
- package/dist/digest.d.ts +7 -0
- package/dist/digest.d.ts.map +1 -0
- package/dist/digest.js +113 -0
- package/dist/durability.d.ts +5 -1
- package/dist/durability.d.ts.map +1 -1
- package/dist/durability.js +5 -0
- package/dist/entry.d.ts +19 -0
- package/dist/entry.d.ts.map +1 -0
- package/dist/entry.js +151 -0
- package/dist/expectation.d.ts +21 -0
- package/dist/expectation.d.ts.map +1 -0
- package/dist/expectation.js +70 -0
- package/dist/fence.d.ts +6 -0
- package/dist/fence.d.ts.map +1 -0
- package/dist/fence.js +10 -0
- package/dist/file.d.ts +2 -19
- package/dist/file.d.ts.map +1 -1
- package/dist/file.js +7 -122
- package/dist/fulltext.d.ts +27 -0
- package/dist/fulltext.d.ts.map +1 -0
- package/dist/fulltext.js +368 -0
- package/dist/host.d.ts +15 -3
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +131 -15
- package/dist/identity.d.ts +3 -0
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js +9 -0
- package/dist/index.d.ts +18 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -0
- package/dist/live.d.ts +22 -0
- package/dist/live.d.ts.map +1 -0
- package/dist/live.js +124 -0
- package/dist/media.d.ts +8 -0
- package/dist/media.d.ts.map +1 -0
- package/dist/media.js +205 -0
- package/dist/memory.d.ts +12 -5
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +112 -24
- package/dist/messages.d.ts +28 -0
- package/dist/messages.d.ts.map +1 -0
- package/dist/messages.js +90 -0
- package/dist/model.d.ts +10 -2
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +17 -2
- 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 +20 -2
- package/dist/observation.d.ts.map +1 -1
- package/dist/observation.js +113 -12
- package/dist/opened.d.ts +22 -0
- package/dist/opened.d.ts.map +1 -0
- package/dist/opened.js +107 -0
- package/dist/outcome.d.ts +52 -1
- package/dist/outcome.d.ts.map +1 -1
- package/dist/outcome.js +46 -0
- package/dist/overlay.d.ts +15 -5
- package/dist/overlay.d.ts.map +1 -1
- package/dist/overlay.js +129 -177
- package/dist/query.d.ts +68 -4
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +48 -7
- package/dist/reach.d.ts +17 -4
- package/dist/reach.d.ts.map +1 -1
- package/dist/reach.js +226 -50
- package/dist/react.d.ts +10 -7
- package/dist/react.d.ts.map +1 -1
- package/dist/react.js +7 -17
- package/dist/readiness.d.ts +64 -0
- package/dist/readiness.d.ts.map +1 -0
- package/dist/readiness.js +33 -0
- package/dist/reads.d.ts +11 -0
- package/dist/reads.d.ts.map +1 -0
- package/dist/reads.js +100 -0
- package/dist/recovery.d.ts +25 -0
- package/dist/recovery.d.ts.map +1 -0
- package/dist/recovery.js +198 -0
- package/dist/refresh.d.ts +16 -15
- package/dist/refresh.d.ts.map +1 -1
- package/dist/refresh.js +113 -54
- package/dist/replica.d.ts +59 -33
- package/dist/replica.d.ts.map +1 -1
- package/dist/replica.js +309 -157
- package/dist/room-draft.d.ts +35 -3
- package/dist/room-draft.d.ts.map +1 -1
- package/dist/room-draft.js +174 -16
- package/dist/rows.d.ts +37 -35
- package/dist/rows.d.ts.map +1 -1
- package/dist/rows.js +173 -102
- package/dist/save.d.ts +68 -3
- package/dist/save.d.ts.map +1 -1
- package/dist/save.js +795 -143
- package/dist/search.d.ts +39 -28
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +37 -105
- package/dist/seed.d.ts +16 -0
- package/dist/seed.d.ts.map +1 -0
- package/dist/seed.js +75 -0
- package/dist/shared.d.ts +16 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +431 -0
- package/dist/status.d.ts +4 -1
- package/dist/status.d.ts.map +1 -1
- package/dist/status.js +0 -8
- package/dist/store.d.ts +126 -80
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +371 -457
- package/dist/stored.d.ts +152 -0
- package/dist/stored.d.ts.map +1 -0
- package/dist/stored.js +571 -0
- package/dist/tables.d.ts +354 -0
- package/dist/tables.d.ts.map +1 -0
- package/dist/tables.js +624 -0
- package/dist/turn.d.ts +9 -0
- package/dist/turn.d.ts.map +1 -0
- package/dist/turn.js +75 -0
- package/dist/validation.d.ts +30 -0
- package/dist/validation.d.ts.map +1 -0
- package/dist/validation.js +162 -0
- package/dist/windows.d.ts +22 -0
- package/dist/windows.d.ts.map +1 -0
- package/dist/windows.js +89 -0
- package/dist/wire.d.ts +169 -30
- package/dist/wire.d.ts.map +1 -1
- package/dist/wire.js +57 -29
- package/dist/words.d.ts +19 -0
- package/dist/words.d.ts.map +1 -0
- package/dist/words.js +150 -0
- package/package.json +26 -15
- package/AGENTS.md +0 -269
- package/dist/attachment.d.ts +0 -3
- package/dist/attachment.d.ts.map +0 -1
- package/dist/attachment.js +0 -8
- package/dist/read-side.d.ts +0 -18
- package/dist/read-side.d.ts.map +0 -1
- package/dist/read-side.js +0 -433
package/dist/save.js
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import { definitionOf } from './model.js';
|
|
2
|
-
import { apply, ATTACHMENT, links, parse, pointerSegments, references, relative, relativize, retarget, serialize, target, } from '@agentshouse/mdmodel';
|
|
3
|
-
import { immutable, } from './change.js';
|
|
4
|
-
import { collectionOf, documentsOf, governing,
|
|
2
|
+
import { apply, ARRAY_INDEX, ATTACHMENT, links, parse, pointerSegments, references, relative, relativize, retarget, roomReferences, serialize, target, } from '@agentshouse/mdmodel';
|
|
3
|
+
import { immutable, leaving, queuedFile, departure, } from './change.js';
|
|
4
|
+
import { collectionOf, documentsOf, governing, } from './collection.js';
|
|
5
5
|
import { changeBatchIdentity } from './identity.js';
|
|
6
|
-
import {
|
|
6
|
+
import { overlaying } from './overlay.js';
|
|
7
7
|
import { covered, normalize } from './paths.js';
|
|
8
|
+
import { sorting } from './shared.js';
|
|
9
|
+
import { paced, ROW_WEIGHT, stepped } from './turn.js';
|
|
10
|
+
import { validatedCollection } from './validation.js';
|
|
8
11
|
function refuse(code, file, location) {
|
|
9
12
|
return {
|
|
10
13
|
kind: 'refused',
|
|
@@ -20,7 +23,7 @@ function repointed(document, definition, from, to, moves, settled) {
|
|
|
20
23
|
const parsed = parse(document.content, from);
|
|
21
24
|
if (!parsed.ok)
|
|
22
25
|
return [];
|
|
23
|
-
|
|
26
|
+
const changes = [];
|
|
24
27
|
const names = new Set();
|
|
25
28
|
for (const { pointer, value } of definition === undefined
|
|
26
29
|
? []
|
|
@@ -32,38 +35,39 @@ function repointed(document, definition, from, to, moves, settled) {
|
|
|
32
35
|
const destination = moves.get(named) ?? named;
|
|
33
36
|
if (target(to, value) === destination)
|
|
34
37
|
continue;
|
|
35
|
-
|
|
38
|
+
changes.push({
|
|
36
39
|
kind: 'reference',
|
|
37
40
|
pointer,
|
|
38
41
|
value: retarget(value, relative(to, destination)),
|
|
39
42
|
prior: value,
|
|
40
43
|
});
|
|
41
|
-
if (applied.outcome !== 'applied')
|
|
42
|
-
continue;
|
|
43
|
-
next = applied.document;
|
|
44
44
|
names.add(name);
|
|
45
45
|
}
|
|
46
|
+
if (changes.length === 0)
|
|
47
|
+
return [];
|
|
48
|
+
const applied = apply(parsed.document, changes);
|
|
49
|
+
if (applied.outcome !== 'applied')
|
|
50
|
+
return [];
|
|
51
|
+
const next = applied.document;
|
|
46
52
|
return [...names].map((name) => ({ kind: 'field', path: from, name, value: next.fields[name] }));
|
|
47
53
|
}
|
|
48
|
-
function edited(document,
|
|
49
|
-
|
|
54
|
+
function edited(document, changes) {
|
|
55
|
+
const parts = new Map();
|
|
50
56
|
for (const change of changes) {
|
|
51
|
-
if (change.
|
|
52
|
-
|
|
53
|
-
if (change.kind
|
|
54
|
-
|
|
55
|
-
const applied = apply(next, change.kind === 'preamble'
|
|
56
|
-
? { kind: 'preamble', value: change.value, prior: next.preamble }
|
|
57
|
-
: {
|
|
57
|
+
if (change.kind === 'preamble')
|
|
58
|
+
parts.set('preamble', { kind: 'preamble', value: change.value, prior: document.preamble });
|
|
59
|
+
if (change.kind === 'section')
|
|
60
|
+
parts.set(`section ${change.name}`, {
|
|
58
61
|
kind: 'section',
|
|
59
62
|
name: change.name,
|
|
60
63
|
value: change.value,
|
|
61
|
-
prior:
|
|
64
|
+
prior: document.sections[change.name] ?? '',
|
|
62
65
|
});
|
|
63
|
-
if (applied.outcome === 'applied')
|
|
64
|
-
next = applied.document;
|
|
65
66
|
}
|
|
66
|
-
|
|
67
|
+
if (parts.size === 0)
|
|
68
|
+
return document;
|
|
69
|
+
const applied = apply(document, [...parts.values()]);
|
|
70
|
+
return applied.outcome === 'applied' ? applied.document : document;
|
|
67
71
|
}
|
|
68
72
|
function bodies(path, before, after) {
|
|
69
73
|
const changes = [];
|
|
@@ -78,7 +82,7 @@ function bodies(path, before, after) {
|
|
|
78
82
|
return changes;
|
|
79
83
|
}
|
|
80
84
|
function relinks(path, moved, before, moves) {
|
|
81
|
-
|
|
85
|
+
const changes = [];
|
|
82
86
|
for (const link of links(before, path)) {
|
|
83
87
|
const destination = moves.get(link.target) ?? link.target;
|
|
84
88
|
if (destination === link.target && moved === path)
|
|
@@ -86,20 +90,23 @@ function relinks(path, moved, before, moves) {
|
|
|
86
90
|
const to = relative(moved, destination);
|
|
87
91
|
if (to === relative(path, link.target))
|
|
88
92
|
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;
|
|
93
|
+
changes.push({ kind: 'link', href: link.href, to });
|
|
102
94
|
}
|
|
95
|
+
if (changes.length === 0)
|
|
96
|
+
return { kind: 'changes', changes: [] };
|
|
97
|
+
const applied = apply(before, changes);
|
|
98
|
+
if (applied.outcome === 'refuse') {
|
|
99
|
+
return {
|
|
100
|
+
kind: 'refused',
|
|
101
|
+
refusal: {
|
|
102
|
+
code: applied.refusal.rule,
|
|
103
|
+
retryable: false,
|
|
104
|
+
file: path,
|
|
105
|
+
...(applied.refusal.location === undefined ? {} : { location: applied.refusal.location }),
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
const after = applied.document;
|
|
103
110
|
if (after === before)
|
|
104
111
|
return { kind: 'changes', changes: [] };
|
|
105
112
|
if (before.type === undefined) {
|
|
@@ -107,74 +114,161 @@ function relinks(path, moved, before, moves) {
|
|
|
107
114
|
}
|
|
108
115
|
return { kind: 'changes', changes: bodies(path, before, after) };
|
|
109
116
|
}
|
|
110
|
-
function
|
|
111
|
-
|
|
112
|
-
const item = merged[index];
|
|
113
|
-
if (item.path !== path)
|
|
114
|
-
continue;
|
|
115
|
-
if (item.kind === 'section' || item.kind === 'preamble')
|
|
116
|
-
merged.splice(index, 1);
|
|
117
|
-
}
|
|
117
|
+
function partKey(change) {
|
|
118
|
+
return change.kind === 'section' ? `section ${change.name}` : change.kind;
|
|
118
119
|
}
|
|
119
|
-
function rewrites(changes, visible) {
|
|
120
|
+
function* rewrites(changes, visible) {
|
|
120
121
|
const moves = new Map();
|
|
121
122
|
const settled = new Set();
|
|
122
|
-
|
|
123
|
+
const parts = new Map();
|
|
124
|
+
for (const [index, change] of changes.entries()) {
|
|
123
125
|
if (change.kind === 'move')
|
|
124
126
|
moves.set(change.path, change.to);
|
|
125
|
-
if (change.kind === 'remove' ||
|
|
127
|
+
if (change.kind === 'remove' ||
|
|
128
|
+
change.kind === 'replace' ||
|
|
129
|
+
change.kind === 'create' ||
|
|
130
|
+
change.kind === 'binary') {
|
|
126
131
|
settled.add(change.path);
|
|
127
132
|
}
|
|
128
133
|
if (change.kind === 'field')
|
|
129
134
|
settled.add(`${change.path} ${change.name}`);
|
|
135
|
+
if (isPart(change)) {
|
|
136
|
+
const held = parts.get(change.path);
|
|
137
|
+
if (held === undefined)
|
|
138
|
+
parts.set(change.path, [index]);
|
|
139
|
+
else
|
|
140
|
+
held.push(index);
|
|
141
|
+
}
|
|
130
142
|
}
|
|
131
143
|
if (moves.size === 0)
|
|
132
144
|
return { kind: 'changes', changes };
|
|
133
145
|
const merged = [...changes];
|
|
134
146
|
const added = [];
|
|
135
|
-
for (const [path, document] of documentsOf(visible.collection)) {
|
|
147
|
+
for (const [path, document] of yield* documentsOf(visible.collection)) {
|
|
136
148
|
if (settled.has(path))
|
|
137
149
|
continue;
|
|
138
150
|
const moved = moves.get(path) ?? path;
|
|
139
151
|
const definition = definitionOf(governing(visible.collection.composition, path), document.type);
|
|
140
|
-
|
|
141
|
-
|
|
152
|
+
const repointing = repointed(document, definition, path, moved, moves, settled);
|
|
153
|
+
added.push(...repointing);
|
|
154
|
+
const own = parts.get(path) ?? [];
|
|
155
|
+
const relinked = relinks(path, moved, edited(document, own.map((index) => merged[index])), moves);
|
|
142
156
|
if (relinked.kind === 'refused')
|
|
143
157
|
return relinked;
|
|
158
|
+
const keyed = new Map();
|
|
159
|
+
for (const index of own) {
|
|
160
|
+
const key = partKey(merged[index]);
|
|
161
|
+
if (!keyed.has(key))
|
|
162
|
+
keyed.set(key, index);
|
|
163
|
+
}
|
|
144
164
|
for (const change of relinked.changes) {
|
|
145
165
|
if (change.kind === 'replace') {
|
|
146
|
-
|
|
166
|
+
for (const index of own) {
|
|
167
|
+
const kind = merged[index]?.kind;
|
|
168
|
+
if (kind === 'section' || kind === 'preamble')
|
|
169
|
+
merged[index] = null;
|
|
170
|
+
}
|
|
147
171
|
added.push(change);
|
|
148
172
|
continue;
|
|
149
173
|
}
|
|
150
|
-
const index =
|
|
151
|
-
|
|
152
|
-
(change.kind !== 'section' || (item.kind === 'section' && item.name === change.name)));
|
|
153
|
-
if (index === -1)
|
|
174
|
+
const index = keyed.get(partKey(change));
|
|
175
|
+
if (index === undefined)
|
|
154
176
|
added.push(change);
|
|
155
177
|
else
|
|
156
178
|
merged[index] = change;
|
|
157
179
|
}
|
|
180
|
+
const text = visible.collection.text[path].length;
|
|
181
|
+
const rewritten = repointing.length > 0 || relinked.changes.length > 0;
|
|
182
|
+
yield ROW_WEIGHT + text + (rewritten ? text : 0);
|
|
158
183
|
}
|
|
159
|
-
return {
|
|
184
|
+
return {
|
|
185
|
+
kind: 'changes',
|
|
186
|
+
changes: [...added, ...merged.filter((change) => change !== null)],
|
|
187
|
+
};
|
|
160
188
|
}
|
|
161
|
-
function anticipated(changes, preconditions, rewritten, visible) {
|
|
162
|
-
const
|
|
189
|
+
function* anticipated(changes, preconditions, rewritten, visible) {
|
|
190
|
+
const given = new Set(changes);
|
|
191
|
+
const derived = rewritten.filter((change) => !given.has(change));
|
|
163
192
|
return {
|
|
164
193
|
changes: rewritten,
|
|
165
|
-
preconditions: [...preconditionsOf(derived, visible, []), ...preconditions],
|
|
194
|
+
preconditions: [...(yield* preconditionsOf(derived, visible, [])), ...preconditions],
|
|
166
195
|
};
|
|
167
196
|
}
|
|
168
|
-
export function providerRewritten(batch, visible) {
|
|
169
|
-
const rewritten = rewrites(batch.changes, visible);
|
|
197
|
+
export function* providerRewritten(batch, visible) {
|
|
198
|
+
const rewritten = yield* rewrites(batch.changes, visible);
|
|
170
199
|
if (rewritten.kind === 'refused')
|
|
171
200
|
return batch;
|
|
172
201
|
return {
|
|
173
202
|
...batch,
|
|
174
|
-
...anticipated(batch.changes, batch.preconditions, rewritten.changes, visible),
|
|
203
|
+
...(yield* anticipated(batch.changes, batch.preconditions, rewritten.changes, visible)),
|
|
175
204
|
};
|
|
176
205
|
}
|
|
177
|
-
function
|
|
206
|
+
function crossed(document, definition, path, to) {
|
|
207
|
+
const changes = [];
|
|
208
|
+
for (const link of links(document, path)) {
|
|
209
|
+
const written = to(link.target, link.href);
|
|
210
|
+
if (written !== undefined)
|
|
211
|
+
changes.push({ kind: 'link', href: link.href, to: written });
|
|
212
|
+
}
|
|
213
|
+
for (const { pointer, value } of definition === undefined
|
|
214
|
+
? []
|
|
215
|
+
: references(document, definition)) {
|
|
216
|
+
const named = target(path, value);
|
|
217
|
+
const written = named === null ? undefined : to(named, value);
|
|
218
|
+
if (written !== undefined) {
|
|
219
|
+
changes.push({ kind: 'reference', pointer, value: retarget(value, written), prior: value });
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return changes;
|
|
223
|
+
}
|
|
224
|
+
function rewritten(path, document, changes) {
|
|
225
|
+
if (changes.length === 0)
|
|
226
|
+
return [];
|
|
227
|
+
const applied = apply(document, changes);
|
|
228
|
+
if (applied.outcome !== 'applied')
|
|
229
|
+
return [];
|
|
230
|
+
const after = applied.document;
|
|
231
|
+
if (document.type === undefined)
|
|
232
|
+
return [{ kind: 'replace', path, content: serialize(after) }];
|
|
233
|
+
const names = new Set(changes.flatMap((change) => change.kind === 'reference' ? [pointerSegments(change.pointer)[0] ?? ''] : []));
|
|
234
|
+
return [
|
|
235
|
+
...[...names].map((name) => ({ kind: 'field', path, name, value: after.fields[name] })),
|
|
236
|
+
...bodies(path, document, after),
|
|
237
|
+
];
|
|
238
|
+
}
|
|
239
|
+
function roomPathOf(handle, path) {
|
|
240
|
+
return `/rooms/${handle}/${path}`;
|
|
241
|
+
}
|
|
242
|
+
export function* leavingRewritten(batch, visible) {
|
|
243
|
+
const between = batch.between;
|
|
244
|
+
if (between === undefined)
|
|
245
|
+
return batch;
|
|
246
|
+
const moved = new Map(between.moves.map(({ path, to }) => [path, roomPathOf(between.destination.handle, to)]));
|
|
247
|
+
const derived = [];
|
|
248
|
+
for (const [path, document] of yield* documentsOf(visible.collection)) {
|
|
249
|
+
if (moved.has(path))
|
|
250
|
+
continue;
|
|
251
|
+
const definition = definitionOf(governing(visible.collection.composition, path), document.type);
|
|
252
|
+
derived.push(...rewritten(path, document, crossed(document, definition, path, (named) => moved.get(named))));
|
|
253
|
+
yield ROW_WEIGHT + visible.collection.text[path].length;
|
|
254
|
+
}
|
|
255
|
+
if (derived.length === 0)
|
|
256
|
+
return batch;
|
|
257
|
+
return {
|
|
258
|
+
...batch,
|
|
259
|
+
changes: [...derived, ...batch.changes],
|
|
260
|
+
preconditions: [...(yield* preconditionsOf(derived, visible, [])), ...batch.preconditions],
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
export function rewriting(batch, locator) {
|
|
264
|
+
return leaving(batch, locator) || batch.changes.some((change) => change.kind === 'move');
|
|
265
|
+
}
|
|
266
|
+
export function* effectiveIn(batch, locator, visible) {
|
|
267
|
+
return leaving(batch, locator)
|
|
268
|
+
? yield* leavingRewritten(batch, visible)
|
|
269
|
+
: yield* providerRewritten(batch, visible);
|
|
270
|
+
}
|
|
271
|
+
function* incoming(collection, removed, candidates) {
|
|
178
272
|
const found = new Map();
|
|
179
273
|
const note = (target, file) => {
|
|
180
274
|
if (!removed.has(target))
|
|
@@ -183,7 +277,20 @@ function incoming(collection, removed) {
|
|
|
183
277
|
referrers.add(file);
|
|
184
278
|
found.set(target, referrers);
|
|
185
279
|
};
|
|
186
|
-
|
|
280
|
+
const read = function* (paths) {
|
|
281
|
+
const seen = new Set();
|
|
282
|
+
for (const path of paths) {
|
|
283
|
+
if (seen.has(path))
|
|
284
|
+
continue;
|
|
285
|
+
seen.add(path);
|
|
286
|
+
const document = collection.document(path);
|
|
287
|
+
if (document !== undefined)
|
|
288
|
+
yield [path, document];
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
for (const [path, document] of candidates === null
|
|
292
|
+
? yield* documentsOf(collection)
|
|
293
|
+
: read(candidates)) {
|
|
187
294
|
const definition = definitionOf(governing(collection.composition, path), document.type);
|
|
188
295
|
for (const { value, to } of definition === undefined ? [] : references(document, definition)) {
|
|
189
296
|
if (to.includes(ATTACHMENT))
|
|
@@ -192,8 +299,9 @@ function incoming(collection, removed) {
|
|
|
192
299
|
if (named !== null)
|
|
193
300
|
note(named, path);
|
|
194
301
|
}
|
|
302
|
+
yield ROW_WEIGHT + collection.text[path].length;
|
|
195
303
|
}
|
|
196
|
-
const [first] =
|
|
304
|
+
const [first] = yield* sorting(found.keys());
|
|
197
305
|
if (first === undefined)
|
|
198
306
|
return null;
|
|
199
307
|
const every = new Set();
|
|
@@ -206,19 +314,22 @@ function incoming(collection, removed) {
|
|
|
206
314
|
code: 'referenced-missing-file',
|
|
207
315
|
retryable: false,
|
|
208
316
|
file: first,
|
|
209
|
-
referrers:
|
|
317
|
+
referrers: yield* sorting(every),
|
|
210
318
|
},
|
|
211
319
|
};
|
|
212
320
|
}
|
|
213
321
|
function affected(change) {
|
|
214
322
|
return change.kind === 'move' ? [change.path, change.to] : [change.path];
|
|
215
323
|
}
|
|
216
|
-
function guard(changes, input) {
|
|
217
|
-
const
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
324
|
+
function guard(changes, input, removing) {
|
|
325
|
+
const overridden = new Map();
|
|
326
|
+
const existing = {
|
|
327
|
+
has: (path) => overridden.get(path) ??
|
|
328
|
+
(Object.hasOwn(input.visible.documents, path) || Object.hasOwn(input.visible.binaries, path)),
|
|
329
|
+
add: (path) => void overridden.set(path, true),
|
|
330
|
+
delete: (path) => void overridden.set(path, false),
|
|
331
|
+
};
|
|
332
|
+
for (const [index, change] of changes.entries()) {
|
|
222
333
|
for (const path of affected(change)) {
|
|
223
334
|
const normalized = normalize(path);
|
|
224
335
|
if (normalized === null || normalized === '')
|
|
@@ -226,22 +337,36 @@ function guard(changes, input) {
|
|
|
226
337
|
if (covered(input.providerOwnedPaths, normalized)) {
|
|
227
338
|
return refuse('provider-owned-path', normalized);
|
|
228
339
|
}
|
|
229
|
-
const operation = change.kind === 'remove' ?
|
|
340
|
+
const operation = change.kind === 'remove' ? removing : 'write';
|
|
230
341
|
if (!input.local && !covered(input.operations[operation], normalized))
|
|
231
342
|
return refuse(`${operation}-grant`, normalized);
|
|
232
343
|
}
|
|
233
|
-
if (change.kind === 'create') {
|
|
344
|
+
if (change.kind === 'create' || change.kind === 'arrival') {
|
|
234
345
|
if (existing.has(change.path))
|
|
235
346
|
return refuse('path-collision', change.path);
|
|
236
347
|
existing.add(change.path);
|
|
237
348
|
continue;
|
|
238
349
|
}
|
|
350
|
+
if (change.kind === 'binary') {
|
|
351
|
+
if (input.visible.documents[change.path] !== undefined ||
|
|
352
|
+
(existing.has(change.path) &&
|
|
353
|
+
input.bases.some((base) => base.kind === 'absent' && base.path === change.path))) {
|
|
354
|
+
return refuse('path-collision', change.path);
|
|
355
|
+
}
|
|
356
|
+
if (!input.local &&
|
|
357
|
+
existing.has(change.path) &&
|
|
358
|
+
input.visible.revisions[departure(changes, index)] === undefined) {
|
|
359
|
+
return refuse('unknown-revision', change.path);
|
|
360
|
+
}
|
|
361
|
+
existing.add(change.path);
|
|
362
|
+
continue;
|
|
363
|
+
}
|
|
239
364
|
if (change.kind === 'move') {
|
|
240
365
|
if (!existing.has(change.path))
|
|
241
366
|
return refuse('path-absent', change.path);
|
|
242
367
|
if (existing.has(change.to))
|
|
243
368
|
return refuse('path-collision', change.to);
|
|
244
|
-
if (
|
|
369
|
+
if (unrevised(input, change.path)) {
|
|
245
370
|
return refuse('unknown-revision', change.path);
|
|
246
371
|
}
|
|
247
372
|
existing.delete(change.path);
|
|
@@ -251,14 +376,14 @@ function guard(changes, input) {
|
|
|
251
376
|
if (!existing.has(change.path))
|
|
252
377
|
return refuse('path-absent', change.path);
|
|
253
378
|
if (change.kind === 'remove') {
|
|
254
|
-
if (
|
|
379
|
+
if (unrevised(input, change.path)) {
|
|
255
380
|
return refuse('unknown-revision', change.path);
|
|
256
381
|
}
|
|
257
382
|
existing.delete(change.path);
|
|
258
383
|
continue;
|
|
259
384
|
}
|
|
260
385
|
if (change.kind === 'replace') {
|
|
261
|
-
if (
|
|
386
|
+
if (unrevised(input, change.path)) {
|
|
262
387
|
return refuse('unknown-revision', change.path);
|
|
263
388
|
}
|
|
264
389
|
continue;
|
|
@@ -269,8 +394,28 @@ function guard(changes, input) {
|
|
|
269
394
|
}
|
|
270
395
|
return null;
|
|
271
396
|
}
|
|
272
|
-
function
|
|
273
|
-
return visible.revisions[path] === undefined
|
|
397
|
+
function unrevised(input, path) {
|
|
398
|
+
return (!input.local && input.queued?.has(path) !== true && input.visible.revisions[path] === undefined);
|
|
399
|
+
}
|
|
400
|
+
export function queuedDocuments(queue) {
|
|
401
|
+
const paths = new Set();
|
|
402
|
+
for (const batch of queue) {
|
|
403
|
+
for (const change of batch.changes) {
|
|
404
|
+
if (change.kind === 'binary' || (change.kind === 'arrival' && !('content' in change)))
|
|
405
|
+
continue;
|
|
406
|
+
if (change.kind === 'remove')
|
|
407
|
+
paths.delete(change.path);
|
|
408
|
+
else if (change.kind !== 'move')
|
|
409
|
+
paths.add(change.path);
|
|
410
|
+
else if (paths.delete(change.path))
|
|
411
|
+
paths.add(change.to);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
return paths;
|
|
415
|
+
}
|
|
416
|
+
function identity(precondition) {
|
|
417
|
+
const part = precondition.kind === 'field' || precondition.kind === 'section' ? precondition.name : '';
|
|
418
|
+
return `${precondition.kind} ${precondition.path} ${part}`;
|
|
274
419
|
}
|
|
275
420
|
function existence(visible, path) {
|
|
276
421
|
const revision = visible.revisions[path];
|
|
@@ -285,18 +430,18 @@ function sameTarget(precondition, change) {
|
|
|
285
430
|
return ((precondition.kind !== 'field' && precondition.kind !== 'section') ||
|
|
286
431
|
((change.kind === 'field' || change.kind === 'section') && precondition.name === change.name));
|
|
287
432
|
}
|
|
288
|
-
function preconditionsOf(changes, visible, bases) {
|
|
433
|
+
function* preconditionsOf(changes, visible, bases) {
|
|
289
434
|
const preconditions = [];
|
|
290
435
|
const seen = new Set();
|
|
291
436
|
const add = (precondition) => {
|
|
292
|
-
const
|
|
293
|
-
|
|
294
|
-
if (seen.has(identity))
|
|
437
|
+
const held = identity(precondition);
|
|
438
|
+
if (seen.has(held))
|
|
295
439
|
return;
|
|
296
|
-
seen.add(
|
|
440
|
+
seen.add(held);
|
|
297
441
|
preconditions.push(precondition);
|
|
298
442
|
};
|
|
299
443
|
for (const change of changes) {
|
|
444
|
+
yield ROW_WEIGHT;
|
|
300
445
|
if (change.kind === 'field' || change.kind === 'section' || change.kind === 'preamble') {
|
|
301
446
|
const based = bases.find((base) => sameTarget(base, change));
|
|
302
447
|
if (based !== undefined) {
|
|
@@ -328,6 +473,13 @@ function preconditionsOf(changes, visible, bases) {
|
|
|
328
473
|
add({ kind: 'absent', path: change.path });
|
|
329
474
|
continue;
|
|
330
475
|
}
|
|
476
|
+
if (change.kind === 'binary') {
|
|
477
|
+
const revision = visible.revisions[change.path];
|
|
478
|
+
add(visible.binaries[change.path] === undefined || revision === undefined
|
|
479
|
+
? { kind: 'absent', path: change.path }
|
|
480
|
+
: { kind: 'revision', path: change.path, revision });
|
|
481
|
+
continue;
|
|
482
|
+
}
|
|
331
483
|
add(existence(visible, change.path));
|
|
332
484
|
if (change.kind === 'move')
|
|
333
485
|
add({ kind: 'absent', path: change.to });
|
|
@@ -337,10 +489,13 @@ function preconditionsOf(changes, visible, bases) {
|
|
|
337
489
|
function depth(path) {
|
|
338
490
|
return path.split('/').length;
|
|
339
491
|
}
|
|
340
|
-
function
|
|
341
|
-
const written =
|
|
492
|
+
function* exceeding(limits, changes, effective, resulting) {
|
|
493
|
+
const written = effective.map((change) => (change.kind === 'move' ? change.to : change.path));
|
|
342
494
|
const created = changes
|
|
343
|
-
.filter((change) => change.kind === 'create' ||
|
|
495
|
+
.filter((change) => change.kind === 'create' ||
|
|
496
|
+
change.kind === 'move' ||
|
|
497
|
+
change.kind === 'binary' ||
|
|
498
|
+
change.kind === 'arrival')
|
|
344
499
|
.map((change) => (change.kind === 'move' ? change.to : change.path));
|
|
345
500
|
for (const limit of limits) {
|
|
346
501
|
const value = limit.value;
|
|
@@ -354,9 +509,13 @@ function exceeded(limits, changes, resulting) {
|
|
|
354
509
|
return refuse('provider-limit', undefined, limit.name);
|
|
355
510
|
}
|
|
356
511
|
if (limit.name === 'document-size') {
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
512
|
+
for (const path of written) {
|
|
513
|
+
const text = resulting.documents[path] ?? '';
|
|
514
|
+
if (new TextEncoder().encode(text).byteLength > value) {
|
|
515
|
+
return refuse('provider-limit', path, limit.name);
|
|
516
|
+
}
|
|
517
|
+
yield ROW_WEIGHT + text.length;
|
|
518
|
+
}
|
|
360
519
|
}
|
|
361
520
|
if (limit.name === 'path-length') {
|
|
362
521
|
const long = created.find((path) => path.length > value);
|
|
@@ -386,7 +545,7 @@ function relativeField(path, name, value, handle, definition) {
|
|
|
386
545
|
!opened.ok ||
|
|
387
546
|
!JSON.stringify(value ?? null).includes(`/rooms/${handle}/`))
|
|
388
547
|
return value;
|
|
389
|
-
const held = apply(opened.document, { kind: 'field', name, value, prior: undefined });
|
|
548
|
+
const held = apply(opened.document, [{ kind: 'field', name, value, prior: undefined }]);
|
|
390
549
|
if (held.outcome !== 'applied')
|
|
391
550
|
return value;
|
|
392
551
|
const result = relativize(held.document, path, handle, definition);
|
|
@@ -422,54 +581,356 @@ function relativeChanges(input) {
|
|
|
422
581
|
return { ...change, fields, sections };
|
|
423
582
|
});
|
|
424
583
|
}
|
|
425
|
-
function
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
584
|
+
function unqueueable(file) {
|
|
585
|
+
return { kind: 'refused', refusal: { code: 'unknown-revision', retryable: false, file } };
|
|
586
|
+
}
|
|
587
|
+
function isPart(change) {
|
|
588
|
+
return change.kind === 'field' || change.kind === 'section' || change.kind === 'preamble';
|
|
589
|
+
}
|
|
590
|
+
function touched(change) {
|
|
591
|
+
return change.kind === 'move' ? [change.path, change.to] : [change.path];
|
|
592
|
+
}
|
|
593
|
+
function partsApplied(content, path, changes) {
|
|
594
|
+
const parsed = parse(content, path);
|
|
595
|
+
if (!parsed.ok)
|
|
596
|
+
return null;
|
|
597
|
+
const document = parsed.document;
|
|
598
|
+
const parts = new Map();
|
|
599
|
+
for (const change of changes) {
|
|
600
|
+
if (change.kind === 'field')
|
|
601
|
+
parts.set(`field ${change.name}`, {
|
|
602
|
+
kind: 'field',
|
|
603
|
+
name: change.name,
|
|
604
|
+
value: change.value,
|
|
605
|
+
prior: document.fields[change.name],
|
|
606
|
+
});
|
|
607
|
+
else if (change.kind === 'section')
|
|
608
|
+
parts.set(`section ${change.name}`, {
|
|
609
|
+
kind: 'section',
|
|
610
|
+
name: change.name,
|
|
611
|
+
value: change.value,
|
|
612
|
+
prior: document.sections[change.name] ?? '',
|
|
613
|
+
});
|
|
614
|
+
else
|
|
615
|
+
parts.set('preamble', { kind: 'preamble', value: change.value, prior: document.preamble });
|
|
431
616
|
}
|
|
432
|
-
|
|
617
|
+
const applied = apply(document, [...parts.values()]);
|
|
618
|
+
return applied.outcome === 'applied' ? serialize(applied.document) : null;
|
|
433
619
|
}
|
|
434
|
-
|
|
435
|
-
const
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
620
|
+
function settledParts(changes, deferred) {
|
|
621
|
+
for (const [held, parts] of deferred) {
|
|
622
|
+
const content = partsApplied(held.content, held.path, parts);
|
|
623
|
+
if (content === null)
|
|
624
|
+
return held.path;
|
|
625
|
+
changes[changes.indexOf(held)] = { ...held, content };
|
|
626
|
+
}
|
|
627
|
+
deferred.clear();
|
|
628
|
+
return null;
|
|
629
|
+
}
|
|
630
|
+
function folded(changes, change, appending, deferred) {
|
|
631
|
+
const held = changes.find((item) => item.path === change.path && (item.kind === 'create' || item.kind === 'replace'));
|
|
632
|
+
const whole = held === undefined ? -1 : changes.indexOf(held);
|
|
633
|
+
const occupied = changes.some((item) => !isPart(item) && item !== held && touched(item).includes(change.path));
|
|
634
|
+
if (isPart(change)) {
|
|
635
|
+
const same = changes.findIndex((item) => item.kind === change.kind &&
|
|
636
|
+
item.path === change.path &&
|
|
637
|
+
(change.kind === 'preamble' || ('name' in item && item.name === change.name)));
|
|
638
|
+
if (same !== -1) {
|
|
639
|
+
changes[same] = change;
|
|
640
|
+
return 'folded';
|
|
641
|
+
}
|
|
642
|
+
if (held === undefined) {
|
|
643
|
+
if (!appending)
|
|
644
|
+
return 'skipped';
|
|
645
|
+
if (occupied)
|
|
646
|
+
return 'refused';
|
|
647
|
+
changes.push(change);
|
|
648
|
+
return 'appended';
|
|
649
|
+
}
|
|
650
|
+
if ('content' in held) {
|
|
651
|
+
const parts = deferred.get(held);
|
|
652
|
+
if (parts === undefined)
|
|
653
|
+
deferred.set(held, [change]);
|
|
654
|
+
else
|
|
655
|
+
parts.push(change);
|
|
656
|
+
return 'folded';
|
|
657
|
+
}
|
|
658
|
+
if (change.kind === 'preamble')
|
|
659
|
+
return 'refused';
|
|
660
|
+
changes[whole] =
|
|
661
|
+
change.kind === 'field'
|
|
662
|
+
? { ...held, fields: { ...held.fields, [change.name]: change.value } }
|
|
663
|
+
: { ...held, sections: { ...held.sections, [change.name]: change.value } };
|
|
664
|
+
return 'folded';
|
|
665
|
+
}
|
|
666
|
+
if (change.kind === 'replace') {
|
|
667
|
+
if (held !== undefined) {
|
|
668
|
+
if (!('content' in held))
|
|
669
|
+
return 'refused';
|
|
670
|
+
changes[whole] = { ...held, content: change.content };
|
|
671
|
+
return 'folded';
|
|
672
|
+
}
|
|
673
|
+
const parts = changes.filter((item) => isPart(item) && item.path === change.path);
|
|
674
|
+
if (parts.length === 0 && !appending)
|
|
675
|
+
return 'skipped';
|
|
676
|
+
if (occupied)
|
|
677
|
+
return 'refused';
|
|
678
|
+
changes.splice(0, changes.length, ...changes.filter((item) => !parts.includes(item)), change);
|
|
679
|
+
return 'appended';
|
|
680
|
+
}
|
|
681
|
+
if (changes.some((item) => touched(change).some((path) => touched(item).includes(path))))
|
|
682
|
+
return 'refused';
|
|
683
|
+
changes.push(change);
|
|
684
|
+
return 'appended';
|
|
685
|
+
}
|
|
686
|
+
function* relinked(batch, move, before, after, appending) {
|
|
687
|
+
const derived = yield* rewrites([move], after);
|
|
688
|
+
if (derived.kind === 'refused')
|
|
689
|
+
return derived;
|
|
690
|
+
const changes = [...batch.changes];
|
|
691
|
+
const appended = [];
|
|
692
|
+
let changed = false;
|
|
693
|
+
const deferred = new Map();
|
|
694
|
+
for (const change of derived.changes) {
|
|
695
|
+
if (change === move)
|
|
696
|
+
continue;
|
|
697
|
+
const unsettled = isPart(change) ? null : settledParts(changes, deferred);
|
|
698
|
+
if (unsettled !== null)
|
|
699
|
+
return unqueueable(unsettled);
|
|
700
|
+
const outcome = folded(changes, change, appending, deferred);
|
|
701
|
+
if (outcome === 'refused')
|
|
702
|
+
return unqueueable(change.path);
|
|
703
|
+
if (outcome === 'appended')
|
|
704
|
+
appended.push(change);
|
|
705
|
+
if (outcome !== 'skipped')
|
|
706
|
+
changed = true;
|
|
707
|
+
}
|
|
708
|
+
const unsettled = settledParts(changes, deferred);
|
|
709
|
+
if (unsettled !== null)
|
|
710
|
+
return unqueueable(unsettled);
|
|
711
|
+
if (!changed)
|
|
712
|
+
return { kind: 'relinked', batch };
|
|
713
|
+
const held = new Set(batch.preconditions.map(identity));
|
|
714
|
+
const added = yield* preconditionsOf(appended, before, []);
|
|
715
|
+
return {
|
|
716
|
+
kind: 'relinked',
|
|
717
|
+
batch: immutable({
|
|
718
|
+
...batch,
|
|
719
|
+
id: changeBatchIdentity(),
|
|
720
|
+
changes,
|
|
721
|
+
preconditions: [
|
|
722
|
+
...batch.preconditions,
|
|
723
|
+
...added.filter((precondition) => !held.has(identity(precondition))),
|
|
724
|
+
],
|
|
725
|
+
}),
|
|
726
|
+
};
|
|
727
|
+
}
|
|
728
|
+
function leading(changes, at) {
|
|
729
|
+
const from = departure(changes, at);
|
|
730
|
+
return new Set(changes.filter((change, index) => index < at && change.kind === 'move' && departure(changes, index) === from));
|
|
731
|
+
}
|
|
732
|
+
function created(batch, at) {
|
|
733
|
+
const file = batch.changes[at];
|
|
734
|
+
return (leading(batch.changes, at).size === 0 &&
|
|
735
|
+
batch.preconditions.some((precondition) => precondition.kind === 'absent' && precondition.path === file.path));
|
|
736
|
+
}
|
|
737
|
+
function withdrawnFile(batch, change, at) {
|
|
738
|
+
const file = batch.changes[at];
|
|
739
|
+
const from = departure(batch.changes, at);
|
|
740
|
+
const moves = leading(batch.changes, at);
|
|
741
|
+
const fresh = created(batch, at);
|
|
742
|
+
const vacated = new Set([...moves].map((move) => move.to));
|
|
743
|
+
if (fresh)
|
|
744
|
+
vacated.add(file.path);
|
|
745
|
+
const replaced = fresh
|
|
746
|
+
? change.kind === 'remove'
|
|
747
|
+
? []
|
|
748
|
+
: [{ ...file, path: change.to }]
|
|
749
|
+
: change.kind === 'remove'
|
|
750
|
+
? [{ kind: 'remove', path: from }]
|
|
751
|
+
: change.to === from
|
|
752
|
+
? [{ ...file, path: from }]
|
|
753
|
+
: [
|
|
754
|
+
{ kind: 'move', path: from, to: change.to },
|
|
755
|
+
{ ...file, path: change.to },
|
|
756
|
+
];
|
|
757
|
+
const added = change.kind === 'move' && (fresh || change.to !== from)
|
|
758
|
+
? [{ kind: 'absent', path: change.to }]
|
|
759
|
+
: [];
|
|
760
|
+
const changes = batch.changes.flatMap((item) => item === file ? replaced : item.kind === 'move' && moves.has(item) ? [] : [item]);
|
|
439
761
|
if (changes.length === 0)
|
|
440
|
-
return
|
|
441
|
-
const
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
const
|
|
449
|
-
const
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
const
|
|
762
|
+
return null;
|
|
763
|
+
const preconditions = [
|
|
764
|
+
...added,
|
|
765
|
+
...batch.preconditions.filter((precondition) => precondition.kind !== 'absent' || !vacated.has(precondition.path)),
|
|
766
|
+
];
|
|
767
|
+
return immutable({ ...batch, id: changeBatchIdentity(), changes, preconditions });
|
|
768
|
+
}
|
|
769
|
+
export function* requeuing(queue, changes, visible, changeable, state) {
|
|
770
|
+
const next = [...queue];
|
|
771
|
+
const origins = new Map();
|
|
772
|
+
const withdrawn = new Map();
|
|
773
|
+
const owners = new Set();
|
|
774
|
+
const queued = new Set();
|
|
775
|
+
const kept = [];
|
|
776
|
+
const originOf = (batch) => origins.get(batch) ?? batch.id;
|
|
777
|
+
const open = (batch) => origins.has(batch) || changeable(batch);
|
|
778
|
+
for (const change of changes) {
|
|
779
|
+
const held = visible.binaries[change.path];
|
|
780
|
+
if ((change.kind !== 'move' && change.kind !== 'remove') || held?.state !== 'pending') {
|
|
781
|
+
kept.push(change);
|
|
782
|
+
continue;
|
|
783
|
+
}
|
|
784
|
+
let index = next.length - 1;
|
|
785
|
+
while (index >= 0 && queuedFile(next[index], change.path, held.localId) === -1)
|
|
786
|
+
index -= 1;
|
|
787
|
+
const owner = next[index];
|
|
788
|
+
if (owner === undefined || !open(owner))
|
|
789
|
+
return unqueueable(change.path);
|
|
790
|
+
const following = next.slice(index);
|
|
791
|
+
const rebuilt = [];
|
|
792
|
+
let before = change.kind === 'move' ? yield* state(next.slice(0, index)) : visible;
|
|
793
|
+
for (const [offset, batch] of following.entries()) {
|
|
794
|
+
let current = batch;
|
|
795
|
+
if (change.kind === 'move') {
|
|
796
|
+
const after = yield* state(next.slice(0, index + offset + 1));
|
|
797
|
+
const appending = offset === 0 && created(batch, queuedFile(batch, change.path, held.localId));
|
|
798
|
+
const relinking = yield* relinked(batch, change, before, after, appending);
|
|
799
|
+
if (relinking.kind === 'refused')
|
|
800
|
+
return relinking;
|
|
801
|
+
current = relinking.batch;
|
|
802
|
+
before = after;
|
|
803
|
+
}
|
|
804
|
+
if (current !== batch && (!open(batch) || batch.between !== undefined))
|
|
805
|
+
return unqueueable(change.path);
|
|
806
|
+
rebuilt.push(offset === 0
|
|
807
|
+
? withdrawnFile(current, change, queuedFile(current, change.path, held.localId))
|
|
808
|
+
: current);
|
|
809
|
+
}
|
|
810
|
+
for (const [offset, batch] of following.entries()) {
|
|
811
|
+
const replaced = rebuilt[offset];
|
|
812
|
+
if (replaced === batch)
|
|
813
|
+
continue;
|
|
814
|
+
const origin = originOf(batch);
|
|
815
|
+
withdrawn.set(origin, replaced);
|
|
816
|
+
if (replaced !== null)
|
|
817
|
+
origins.set(replaced, origin);
|
|
818
|
+
}
|
|
819
|
+
owners.add(originOf(owner));
|
|
820
|
+
next.splice(index, following.length, ...rebuilt.filter((batch) => batch !== null));
|
|
821
|
+
queued.add(change.path);
|
|
822
|
+
}
|
|
823
|
+
return {
|
|
824
|
+
kind: 'requeued',
|
|
825
|
+
queue: withdrawn.size === 0 ? queue : next,
|
|
826
|
+
changes: kept,
|
|
827
|
+
queued,
|
|
828
|
+
withdrawn,
|
|
829
|
+
owners,
|
|
830
|
+
};
|
|
831
|
+
}
|
|
832
|
+
function covering(precondition) {
|
|
833
|
+
return precondition.kind === 'field' ||
|
|
834
|
+
precondition.kind === 'section' ||
|
|
835
|
+
precondition.kind === 'preamble'
|
|
836
|
+
? identity(precondition)
|
|
837
|
+
: `path ${precondition.path}`;
|
|
838
|
+
}
|
|
839
|
+
export function joined(queue, owner, extra) {
|
|
840
|
+
const later = new Set(queue.slice(queue.indexOf(owner) + 1).flatMap((batch) => batch.changes.flatMap(touched)));
|
|
841
|
+
const clash = extra.changes.flatMap(touched).find((path) => later.has(path));
|
|
842
|
+
if (clash !== undefined)
|
|
843
|
+
return unqueueable(clash);
|
|
844
|
+
const changes = [...owner.changes];
|
|
845
|
+
const deferred = new Map();
|
|
846
|
+
for (const change of extra.changes) {
|
|
847
|
+
const unsettled = isPart(change) ? null : settledParts(changes, deferred);
|
|
848
|
+
if (unsettled !== null)
|
|
849
|
+
return unqueueable(unsettled);
|
|
850
|
+
if (folded(changes, change, true, deferred) === 'refused')
|
|
851
|
+
return unqueueable(change.path);
|
|
852
|
+
}
|
|
853
|
+
const unsettled = settledParts(changes, deferred);
|
|
854
|
+
if (unsettled !== null)
|
|
855
|
+
return unqueueable(unsettled);
|
|
856
|
+
const held = new Set(owner.preconditions.map(covering));
|
|
857
|
+
const written = new Set(owner.changes.flatMap((change) => change.kind === 'create' || change.kind === 'replace' ? [change.path] : []));
|
|
858
|
+
const preconditions = [
|
|
859
|
+
...owner.preconditions,
|
|
860
|
+
...extra.preconditions.filter((precondition) => {
|
|
861
|
+
const key = covering(precondition);
|
|
862
|
+
return (!held.has(key) && (key === `path ${precondition.path}` || !written.has(precondition.path)));
|
|
863
|
+
}),
|
|
864
|
+
];
|
|
865
|
+
const batch = immutable({
|
|
866
|
+
...owner,
|
|
867
|
+
...(extra.validation === undefined ? {} : { validation: extra.validation }),
|
|
868
|
+
id: changeBatchIdentity(),
|
|
869
|
+
changes,
|
|
870
|
+
preconditions,
|
|
871
|
+
});
|
|
872
|
+
return { kind: 'joined', batch, queue: queue.map((item) => (item === owner ? batch : item)) };
|
|
873
|
+
}
|
|
874
|
+
export function* limiting(queue, batch, limits, state) {
|
|
875
|
+
const at = queue.indexOf(batch);
|
|
876
|
+
const rewritten = yield* providerRewritten(batch, yield* state(queue.slice(0, at)));
|
|
877
|
+
const limited = yield* exceeding(limits, batch.changes, rewritten.changes, yield* state(queue.slice(0, at + 1)));
|
|
878
|
+
return limited?.kind === 'refused' ? limited.refusal : null;
|
|
879
|
+
}
|
|
880
|
+
function typedText(content) {
|
|
881
|
+
if (content === undefined)
|
|
882
|
+
return false;
|
|
883
|
+
const parsed = parse(content);
|
|
884
|
+
return parsed.ok && parsed.document.type !== undefined && parsed.document.type !== 'any';
|
|
885
|
+
}
|
|
886
|
+
function touchedLibrary(resulting, touched, composition) {
|
|
887
|
+
const entries = touched
|
|
888
|
+
.flatMap((path) => {
|
|
889
|
+
const content = resulting.documents[path];
|
|
890
|
+
const binary = resulting.binaries[path] !== undefined;
|
|
891
|
+
return content === undefined && !binary ? [] : [{ path, content: content ?? null }];
|
|
892
|
+
})
|
|
893
|
+
.sort((left, right) => (left.path < right.path ? -1 : left.path > right.path ? 1 : 0));
|
|
894
|
+
const contents = new Map(entries.map(({ path, content }) => [path, content]));
|
|
895
|
+
const binaries = new Set(Object.keys(resulting.binaries).filter((path) => resulting.binaries[path] !== undefined));
|
|
896
|
+
return validatedCollection(() => Promise.resolve({ entries, next: null }), async (paths) => {
|
|
897
|
+
const known = new Map();
|
|
898
|
+
const pace = paced();
|
|
899
|
+
for (const path of paths) {
|
|
900
|
+
const content = contents.get(path);
|
|
901
|
+
if (binaries.has(path))
|
|
902
|
+
known.set(path, { type: undefined, binary: true });
|
|
903
|
+
else if (content !== undefined && content !== null) {
|
|
904
|
+
const read = parse(content, path);
|
|
905
|
+
if (read.ok)
|
|
906
|
+
known.set(path, { type: read.document.type ?? 'any', binary: false });
|
|
907
|
+
await pace(content.length + ROW_WEIGHT);
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
return known;
|
|
911
|
+
}, composition);
|
|
912
|
+
}
|
|
913
|
+
async function judging(visible, resulting, composition, validate, removed, touched) {
|
|
453
914
|
if (removed.size > 0) {
|
|
454
|
-
const referenced = incoming(collectionOf(resulting.documents,
|
|
915
|
+
const referenced = await stepped(incoming(collectionOf(resulting.documents, composition), removed, null));
|
|
455
916
|
if (referenced !== null)
|
|
456
917
|
return referenced;
|
|
457
918
|
}
|
|
458
|
-
const
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
const
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
919
|
+
const typedPaths = [];
|
|
920
|
+
const pace = paced();
|
|
921
|
+
for (const path of touched) {
|
|
922
|
+
const before = visible.documents[path];
|
|
923
|
+
const after = resulting.documents[path];
|
|
924
|
+
if (typedText(before) || typedText(after))
|
|
925
|
+
typedPaths.push(path);
|
|
926
|
+
await pace((before?.length ?? 0) + (after?.length ?? 0) + ROW_WEIGHT);
|
|
927
|
+
}
|
|
928
|
+
const withoutModel = typedPaths.find((path) => governing(composition, path).status === 'unavailable');
|
|
467
929
|
if (withoutModel !== undefined)
|
|
468
930
|
return refuse('model-unavailable', withoutModel);
|
|
469
|
-
const
|
|
470
|
-
? resulting
|
|
471
|
-
:
|
|
472
|
-
const verdict = validateCollection(validationText, resulting.binaryTargets, input.composition);
|
|
931
|
+
const verdict = typedPaths.length > 0
|
|
932
|
+
? await validate(resulting, touched)
|
|
933
|
+
: await touchedLibrary(resulting, touched, composition);
|
|
473
934
|
if (verdict.verdict === 'refuse') {
|
|
474
935
|
return {
|
|
475
936
|
kind: 'refused',
|
|
@@ -482,12 +943,92 @@ export function buildBatch(input) {
|
|
|
482
943
|
},
|
|
483
944
|
};
|
|
484
945
|
}
|
|
485
|
-
|
|
946
|
+
return {
|
|
947
|
+
kind: 'judged',
|
|
948
|
+
validated: typedPaths
|
|
949
|
+
.map((path) => governing(composition, path))
|
|
950
|
+
.find((model) => model.status !== 'unavailable'),
|
|
951
|
+
};
|
|
952
|
+
}
|
|
953
|
+
function placed(reference) {
|
|
954
|
+
const key = roomPathOf(reference.handle, reference.path);
|
|
955
|
+
if (reference.kind === 'link')
|
|
956
|
+
return `link ${key}`;
|
|
957
|
+
if (reference.kind === 'item')
|
|
958
|
+
return `section ${reference.section} ${key}`;
|
|
959
|
+
const pointer = reference.pointer
|
|
960
|
+
.split('/')
|
|
961
|
+
.map((segment) => (ARRAY_INDEX.test(segment) ? '-' : segment))
|
|
962
|
+
.join('/');
|
|
963
|
+
return `field ${pointer} ${key}`;
|
|
964
|
+
}
|
|
965
|
+
function roomReferencesIn(path, content, composition) {
|
|
966
|
+
if (content === undefined)
|
|
967
|
+
return [];
|
|
968
|
+
const parsed = parse(content, path);
|
|
969
|
+
if (!parsed.ok)
|
|
970
|
+
return [];
|
|
971
|
+
return roomReferences(path, content, definitionOf(governing(composition, path), parsed.document.type));
|
|
972
|
+
}
|
|
973
|
+
function* admittedRooms(changes, visible, resulting, composition) {
|
|
974
|
+
const from = new Map();
|
|
975
|
+
for (const [index, change] of changes.entries()) {
|
|
976
|
+
if (change.kind === 'move')
|
|
977
|
+
from.set(change.to, departure(changes, index));
|
|
978
|
+
}
|
|
979
|
+
for (const path of [...new Set(changes.flatMap(affected))].sort()) {
|
|
980
|
+
const now = resulting.documents[path];
|
|
981
|
+
if (now === undefined)
|
|
982
|
+
continue;
|
|
983
|
+
const held = new Set(roomReferencesIn(path, visible.documents[from.get(path) ?? path], composition).map(placed));
|
|
984
|
+
for (const reference of roomReferencesIn(path, now, composition)) {
|
|
985
|
+
if (reference.kind === 'link' || reference.crossRoom === true)
|
|
986
|
+
continue;
|
|
987
|
+
if (held.has(placed(reference)))
|
|
988
|
+
continue;
|
|
989
|
+
return refuse('unresolved-reference', path, reference.kind === 'field' ? reference.pointer : reference.section);
|
|
990
|
+
}
|
|
991
|
+
yield ROW_WEIGHT + now.length;
|
|
992
|
+
}
|
|
993
|
+
return null;
|
|
994
|
+
}
|
|
995
|
+
function validationOf(validated) {
|
|
996
|
+
return validated === undefined
|
|
997
|
+
? {}
|
|
998
|
+
: {
|
|
999
|
+
validation: {
|
|
1000
|
+
identity: validated.snapshot.identity,
|
|
1001
|
+
version: validated.snapshot.version,
|
|
1002
|
+
source: validated.status,
|
|
1003
|
+
},
|
|
1004
|
+
};
|
|
1005
|
+
}
|
|
1006
|
+
async function batching(input) {
|
|
1007
|
+
const changes = relativeChanges(input);
|
|
1008
|
+
const structural = changes.find((change) => change.kind === 'move' || change.kind === 'remove');
|
|
1009
|
+
if (structural !== undefined && input.unmodeled) {
|
|
1010
|
+
return refuse('model-unavailable', structural.path);
|
|
1011
|
+
}
|
|
1012
|
+
if (changes.length === 0)
|
|
1013
|
+
return refuse('no-change');
|
|
1014
|
+
const rewritten = await stepped(rewrites(changes, input.visible));
|
|
1015
|
+
if (rewritten.kind === 'refused')
|
|
1016
|
+
return rewritten;
|
|
1017
|
+
const refusal = guard(changes, input, 'delete');
|
|
1018
|
+
if (refusal !== null)
|
|
1019
|
+
return refusal;
|
|
1020
|
+
const preconditions = await stepped(preconditionsOf(changes, input.visible, input.bases));
|
|
1021
|
+
const predicted = await stepped(anticipated(changes, preconditions, rewritten.changes, input.visible));
|
|
1022
|
+
const resulting = await stepped(overlaying(input.visible, [{ id: '', baseRoomVersion: input.baseRoomVersion, ...predicted }]));
|
|
1023
|
+
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))]);
|
|
1024
|
+
if (judged.kind === 'refused')
|
|
1025
|
+
return judged;
|
|
1026
|
+
const crossing = await stepped(admittedRooms(changes, input.visible, resulting, input.composition));
|
|
1027
|
+
if (crossing !== null)
|
|
1028
|
+
return crossing;
|
|
1029
|
+
const limited = await stepped(exceeding(input.providerLimits, changes, predicted.changes, resulting));
|
|
486
1030
|
if (limited !== null)
|
|
487
1031
|
return limited;
|
|
488
|
-
const validated = typedPaths
|
|
489
|
-
.map((path) => governing(input.composition, path))
|
|
490
|
-
.find((model) => model.status !== 'unavailable');
|
|
491
1032
|
return {
|
|
492
1033
|
kind: 'batch',
|
|
493
1034
|
batch: immutable({
|
|
@@ -496,15 +1037,126 @@ export function buildBatch(input) {
|
|
|
496
1037
|
changes,
|
|
497
1038
|
preconditions,
|
|
498
1039
|
...(input.writtenBy === undefined ? {} : { writtenBy: input.writtenBy }),
|
|
499
|
-
...(validated
|
|
500
|
-
? {}
|
|
501
|
-
: {
|
|
502
|
-
validation: {
|
|
503
|
-
identity: validated.snapshot.identity,
|
|
504
|
-
version: validated.snapshot.version,
|
|
505
|
-
source: validated.status,
|
|
506
|
-
},
|
|
507
|
-
}),
|
|
1040
|
+
...validationOf(judged.validated),
|
|
508
1041
|
}),
|
|
509
1042
|
};
|
|
510
1043
|
}
|
|
1044
|
+
export const MOVE_DOCUMENTS = 'move-documents';
|
|
1045
|
+
function heldAt(visible, path) {
|
|
1046
|
+
return visible.documents[path] !== undefined || visible.binaries[path] !== undefined;
|
|
1047
|
+
}
|
|
1048
|
+
function arrivingText(path, to, source, moved) {
|
|
1049
|
+
const content = source.visible.documents[path];
|
|
1050
|
+
const document = source.visible.collection.document(path);
|
|
1051
|
+
if (document === undefined)
|
|
1052
|
+
return content;
|
|
1053
|
+
const definition = definitionOf(governing(source.composition, path), document.type);
|
|
1054
|
+
const changes = crossed(document, definition, path, (named, href) => {
|
|
1055
|
+
const at = moved.get(named);
|
|
1056
|
+
if (at !== undefined)
|
|
1057
|
+
return target(to, href) === at ? undefined : relative(to, at);
|
|
1058
|
+
return heldAt(source.visible, named) ? roomPathOf(source.handle, named) : undefined;
|
|
1059
|
+
});
|
|
1060
|
+
if (changes.length === 0)
|
|
1061
|
+
return content;
|
|
1062
|
+
const applied = apply(document, changes);
|
|
1063
|
+
return applied.outcome === 'applied' ? serialize(applied.document) : content;
|
|
1064
|
+
}
|
|
1065
|
+
function arrivalOf({ path, to }, source, moved) {
|
|
1066
|
+
const { documents, revisions, fileIds, changes, binaries } = source.visible;
|
|
1067
|
+
const kept = {
|
|
1068
|
+
...(fileIds[path] === undefined ? {} : { fileId: fileIds[path] }),
|
|
1069
|
+
...(changes[path] === undefined ? {} : { change: changes[path] }),
|
|
1070
|
+
};
|
|
1071
|
+
const heads = {
|
|
1072
|
+
...(revisions[path] === undefined ? {} : { revision: revisions[path] }),
|
|
1073
|
+
...kept,
|
|
1074
|
+
};
|
|
1075
|
+
const binary = binaries[path];
|
|
1076
|
+
if (binary?.state === 'accepted') {
|
|
1077
|
+
const { byteLength, mediaType, sha256 } = binary;
|
|
1078
|
+
return {
|
|
1079
|
+
kind: 'arrival',
|
|
1080
|
+
path: to,
|
|
1081
|
+
binary: { byteLength, mediaType, sha256 },
|
|
1082
|
+
...heads,
|
|
1083
|
+
};
|
|
1084
|
+
}
|
|
1085
|
+
const content = arrivingText(path, to, source, moved);
|
|
1086
|
+
return {
|
|
1087
|
+
kind: 'arrival',
|
|
1088
|
+
path: to,
|
|
1089
|
+
content,
|
|
1090
|
+
...(content === documents[path] ? heads : kept),
|
|
1091
|
+
};
|
|
1092
|
+
}
|
|
1093
|
+
async function moving(input) {
|
|
1094
|
+
const { source, destination, moves } = input;
|
|
1095
|
+
if (moves.length === 0)
|
|
1096
|
+
return refuse('no-change');
|
|
1097
|
+
const removals = moves.map(({ path }) => ({ kind: 'remove', path }));
|
|
1098
|
+
const leaves = guard(removals, { ...source, local: input.local, bases: [] }, 'write');
|
|
1099
|
+
if (leaves !== null)
|
|
1100
|
+
return leaves;
|
|
1101
|
+
const uploading = moves.find(({ path }) => source.visible.binaries[path]?.state === 'pending');
|
|
1102
|
+
if (uploading !== undefined)
|
|
1103
|
+
return refuse('unknown-revision', uploading.path);
|
|
1104
|
+
const moved = new Map(moves.map(({ path, to }) => [path, to]));
|
|
1105
|
+
const arrivals = moves.map((move) => arrivalOf(move, source, moved));
|
|
1106
|
+
const arrives = guard(arrivals, { ...destination, local: input.local, bases: [] }, 'write');
|
|
1107
|
+
if (arrives !== null)
|
|
1108
|
+
return arrives;
|
|
1109
|
+
const bound = input.providerLimits.find((limit) => limit.name === MOVE_DOCUMENTS)?.value;
|
|
1110
|
+
if (typeof bound === 'number' && moves.length > bound) {
|
|
1111
|
+
return refuse('provider-limit', undefined, MOVE_DOCUMENTS);
|
|
1112
|
+
}
|
|
1113
|
+
if (source.unmodeled)
|
|
1114
|
+
return refuse('model-unavailable', moves[0].path);
|
|
1115
|
+
const id = changeBatchIdentity();
|
|
1116
|
+
const between = {
|
|
1117
|
+
source: { room: source.room, baseRoomVersion: source.baseRoomVersion },
|
|
1118
|
+
destination: {
|
|
1119
|
+
room: destination.room,
|
|
1120
|
+
baseRoomVersion: destination.baseRoomVersion,
|
|
1121
|
+
handle: destination.handle,
|
|
1122
|
+
},
|
|
1123
|
+
moves,
|
|
1124
|
+
};
|
|
1125
|
+
const written = input.writtenBy === undefined ? {} : { writtenBy: input.writtenBy };
|
|
1126
|
+
const leavingBatch = {
|
|
1127
|
+
id,
|
|
1128
|
+
baseRoomVersion: source.baseRoomVersion,
|
|
1129
|
+
changes: removals,
|
|
1130
|
+
preconditions: moves.map(({ path }) => existence(source.visible, path)),
|
|
1131
|
+
...written,
|
|
1132
|
+
between,
|
|
1133
|
+
};
|
|
1134
|
+
const effective = await stepped(leavingRewritten(leavingBatch, source.visible));
|
|
1135
|
+
const left = await stepped(overlaying(source.visible, [effective]));
|
|
1136
|
+
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))]);
|
|
1137
|
+
if (sourceJudged.kind === 'refused')
|
|
1138
|
+
return sourceJudged;
|
|
1139
|
+
const arrivingBatch = {
|
|
1140
|
+
id,
|
|
1141
|
+
baseRoomVersion: destination.baseRoomVersion,
|
|
1142
|
+
changes: arrivals,
|
|
1143
|
+
preconditions: moves.map(({ to }) => ({ kind: 'absent', path: to })),
|
|
1144
|
+
...written,
|
|
1145
|
+
between,
|
|
1146
|
+
};
|
|
1147
|
+
const arrived = await stepped(overlaying(destination.visible, [arrivingBatch]));
|
|
1148
|
+
const destinationJudged = await judging(destination.visible, arrived, destination.composition, destination.validate, new Set(), moves.map(({ to }) => to));
|
|
1149
|
+
if (destinationJudged.kind === 'refused')
|
|
1150
|
+
return destinationJudged;
|
|
1151
|
+
const limited = (await stepped(exceeding(input.providerLimits, removals, effective.changes, left))) ??
|
|
1152
|
+
(await stepped(exceeding(input.providerLimits, arrivals, arrivals, arrived)));
|
|
1153
|
+
if (limited !== null)
|
|
1154
|
+
return limited;
|
|
1155
|
+
return { kind: 'moved', leaving: immutable(leavingBatch), arriving: immutable(arrivingBatch) };
|
|
1156
|
+
}
|
|
1157
|
+
export function buildMove(input) {
|
|
1158
|
+
return moving(input);
|
|
1159
|
+
}
|
|
1160
|
+
export function buildBatch(input) {
|
|
1161
|
+
return batching(input);
|
|
1162
|
+
}
|