@agentshouse/mdruntime 0.1.0-ce531b51ff088 → 0.1.0-ce9f29a01a271
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/AGENTS.md +280 -188
- package/README.md +21 -11
- package/dist/adapter.d.ts +156 -42
- package/dist/adapter.d.ts.map +1 -1
- package/dist/adapter.js +1 -9
- package/dist/attached-model.d.ts +11 -0
- package/dist/attached-model.d.ts.map +1 -0
- package/dist/attached-model.js +47 -0
- 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 +23 -0
- package/dist/binary.d.ts.map +1 -0
- package/dist/binary.js +8 -0
- package/dist/bootstrap.d.ts +21 -6
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +114 -37
- package/dist/change-set.d.ts +7 -2
- package/dist/change-set.d.ts.map +1 -1
- package/dist/change-set.js +54 -16
- package/dist/change.d.ts +15 -0
- package/dist/change.d.ts.map +1 -1
- package/dist/change.js +26 -1
- package/dist/chat/acts.d.ts +33 -0
- package/dist/chat/acts.d.ts.map +1 -0
- package/dist/chat/acts.js +36 -0
- package/dist/chat/context.d.ts +3 -0
- package/dist/chat/context.d.ts.map +1 -0
- package/dist/chat/context.js +10 -0
- package/dist/chat/earlier.d.ts +10 -0
- package/dist/chat/earlier.d.ts.map +1 -0
- package/dist/chat/earlier.js +54 -0
- package/dist/chat/input.d.ts +9 -0
- package/dist/chat/input.d.ts.map +1 -0
- package/dist/chat/input.js +99 -0
- package/dist/chat/kit.d.ts +61 -0
- package/dist/chat/kit.d.ts.map +1 -0
- package/dist/chat/kit.js +123 -0
- package/dist/chat/lasting.d.ts +6 -0
- package/dist/chat/lasting.d.ts.map +1 -0
- package/dist/chat/lasting.js +19 -0
- package/dist/chat/list.d.ts +28 -0
- package/dist/chat/list.d.ts.map +1 -0
- package/dist/chat/list.js +99 -0
- package/dist/chat/markdown.d.ts +21 -0
- package/dist/chat/markdown.d.ts.map +1 -0
- package/dist/chat/markdown.js +246 -0
- package/dist/chat/message.d.ts +6 -0
- package/dist/chat/message.d.ts.map +1 -0
- package/dist/chat/message.js +199 -0
- package/dist/chat/messages.d.ts +22 -0
- package/dist/chat/messages.d.ts.map +1 -0
- package/dist/chat/messages.js +175 -0
- package/dist/chat/name.d.ts +6 -0
- package/dist/chat/name.d.ts.map +1 -0
- package/dist/chat/name.js +37 -0
- package/dist/chat/opened.d.ts +11 -0
- package/dist/chat/opened.d.ts.map +1 -0
- package/dist/chat/opened.js +17 -0
- package/dist/chat/overlay.d.ts +32 -0
- package/dist/chat/overlay.d.ts.map +1 -0
- package/dist/chat/overlay.js +107 -0
- package/dist/chat/parts.d.ts +57 -0
- package/dist/chat/parts.d.ts.map +1 -0
- package/dist/chat/parts.js +119 -0
- package/dist/chat/pieces.d.ts +62 -0
- package/dist/chat/pieces.d.ts.map +1 -0
- package/dist/chat/pieces.js +1110 -0
- package/dist/chat/process.d.ts +20 -0
- package/dist/chat/process.d.ts.map +1 -0
- package/dist/chat/process.js +32 -0
- package/dist/chat/provider.d.ts +64 -0
- package/dist/chat/provider.d.ts.map +1 -0
- package/dist/chat/provider.js +637 -0
- package/dist/chat/question.d.ts +9 -0
- package/dist/chat/question.d.ts.map +1 -0
- package/dist/chat/question.js +69 -0
- package/dist/chat/questions.d.ts +31 -0
- package/dist/chat/questions.d.ts.map +1 -0
- package/dist/chat/questions.js +111 -0
- package/dist/chat/read.d.ts +5 -0
- package/dist/chat/read.d.ts.map +1 -0
- package/dist/chat/read.js +13 -0
- package/dist/chat/ready.d.ts +34 -0
- package/dist/chat/ready.d.ts.map +1 -0
- package/dist/chat/ready.js +91 -0
- package/dist/chat/steady.d.ts +3 -0
- package/dist/chat/steady.d.ts.map +1 -0
- package/dist/chat/steady.js +26 -0
- package/dist/chat/style.d.ts +4 -0
- package/dist/chat/style.d.ts.map +1 -0
- package/dist/chat/style.js +302 -0
- package/dist/chat.d.ts +14 -0
- package/dist/chat.d.ts.map +1 -0
- package/dist/chat.js +8 -0
- package/dist/client.d.ts +32 -14
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +82 -26
- package/dist/collection.d.ts +23 -13
- package/dist/collection.d.ts.map +1 -1
- package/dist/collection.js +349 -36
- package/dist/connection.d.ts +56 -24
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +2292 -759
- package/dist/conversation.d.ts +52 -14
- package/dist/conversation.d.ts.map +1 -1
- package/dist/conversation.js +723 -237
- package/dist/durability.d.ts +7 -1
- package/dist/durability.d.ts.map +1 -1
- package/dist/entry.d.ts +17 -0
- package/dist/entry.d.ts.map +1 -0
- package/dist/entry.js +157 -0
- package/dist/expectation.d.ts +21 -0
- package/dist/expectation.d.ts.map +1 -0
- package/dist/expectation.js +70 -0
- package/dist/failure.d.ts +13 -0
- package/dist/failure.d.ts.map +1 -0
- package/dist/failure.js +19 -0
- package/dist/fence.d.ts +6 -0
- package/dist/fence.d.ts.map +1 -0
- package/dist/fence.js +10 -0
- package/dist/host.d.ts +21 -2
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +131 -45
- package/dist/identity.d.ts +5 -0
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js +19 -1
- package/dist/index.d.ts +16 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -0
- package/dist/memory.d.ts +20 -7
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +165 -46
- package/dist/model.d.ts +17 -14
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +16 -4
- package/dist/observation.d.ts +8 -1
- package/dist/observation.d.ts.map +1 -1
- package/dist/observation.js +44 -17
- package/dist/open.d.ts +2 -0
- package/dist/open.d.ts.map +1 -0
- package/dist/open.js +1 -0
- package/dist/outcome.d.ts +38 -3
- package/dist/outcome.d.ts.map +1 -1
- package/dist/outcome.js +45 -1
- package/dist/overlay.d.ts +18 -5
- package/dist/overlay.d.ts.map +1 -1
- package/dist/overlay.js +247 -165
- package/dist/paths.d.ts +0 -1
- package/dist/paths.d.ts.map +1 -1
- package/dist/paths.js +0 -16
- package/dist/query.d.ts +68 -7
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +114 -43
- package/dist/reach.d.ts +45 -0
- package/dist/reach.d.ts.map +1 -0
- package/dist/reach.js +319 -0
- package/dist/react.d.ts +43 -0
- package/dist/react.d.ts.map +1 -0
- package/dist/react.js +54 -0
- package/dist/read-side.d.ts +19 -6
- package/dist/read-side.d.ts.map +1 -1
- package/dist/read-side.js +607 -152
- package/dist/readiness.d.ts +53 -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 +98 -0
- package/dist/recovery.d.ts +25 -0
- package/dist/recovery.d.ts.map +1 -0
- package/dist/recovery.js +197 -0
- package/dist/refresh.d.ts +13 -2
- package/dist/refresh.d.ts.map +1 -1
- package/dist/refresh.js +74 -40
- package/dist/replica.d.ts +17 -8
- package/dist/replica.d.ts.map +1 -1
- package/dist/replica.js +167 -82
- package/dist/room-draft.d.ts +41 -9
- package/dist/room-draft.d.ts.map +1 -1
- package/dist/room-draft.js +146 -36
- package/dist/rows.d.ts +12 -14
- package/dist/rows.d.ts.map +1 -1
- package/dist/rows.js +92 -50
- package/dist/save.d.ts +36 -5
- package/dist/save.d.ts.map +1 -1
- package/dist/save.js +604 -147
- package/dist/search.d.ts +52 -8
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +192 -47
- 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 +15 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +398 -0
- package/dist/status.d.ts +6 -2
- package/dist/status.d.ts.map +1 -1
- package/dist/status.js +0 -8
- package/dist/store.d.ts +92 -19
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +704 -283
- package/dist/turn.d.ts +9 -0
- package/dist/turn.d.ts.map +1 -0
- package/dist/turn.js +64 -0
- package/dist/windows.d.ts +26 -0
- package/dist/windows.d.ts.map +1 -0
- package/dist/windows.js +93 -0
- package/dist/wire.d.ts +178 -19
- package/dist/wire.d.ts.map +1 -1
- package/dist/wire.js +86 -61
- package/package.json +31 -7
- package/dist/attachment.d.ts +0 -3
- package/dist/attachment.d.ts.map +0 -1
- package/dist/attachment.js +0 -8
package/dist/save.js
CHANGED
|
@@ -1,55 +1,72 @@
|
|
|
1
1
|
import { definitionOf } from './model.js';
|
|
2
|
-
import { apply, links, parse, retarget, serialize, target, } from '@agentshouse/mdmodel';
|
|
3
|
-
import { immutable, } from './change.js';
|
|
4
|
-
import { governing,
|
|
2
|
+
import { apply, ATTACHMENT, links, parse, pointerSegments, references, relative, relativize, retarget, serialize, target, } from '@agentshouse/mdmodel';
|
|
3
|
+
import { immutable, queuedFile, departure, } from './change.js';
|
|
4
|
+
import { available, changedCleanly, collectionOf, documentsOf, governing, referring, validateCollection, } from './collection.js';
|
|
5
5
|
import { changeBatchIdentity } from './identity.js';
|
|
6
|
-
import {
|
|
7
|
-
import { covered, normalize
|
|
8
|
-
|
|
6
|
+
import { overlaying } from './overlay.js';
|
|
7
|
+
import { covered, normalize } from './paths.js';
|
|
8
|
+
import { differences } from './shared.js';
|
|
9
|
+
import { finished, ROW_WEIGHT, stepped } from './turn.js';
|
|
10
|
+
function refuse(code, file, location) {
|
|
9
11
|
return {
|
|
10
12
|
kind: 'refused',
|
|
11
13
|
refusal: {
|
|
12
|
-
|
|
14
|
+
code,
|
|
15
|
+
retryable: false,
|
|
13
16
|
...(file === undefined ? {} : { file }),
|
|
14
17
|
...(location === undefined ? {} : { location }),
|
|
15
18
|
},
|
|
16
19
|
};
|
|
17
20
|
}
|
|
18
|
-
function
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
return undefined;
|
|
31
|
-
const destination = moves.get(named) ?? named;
|
|
32
|
-
return target(to, value) === destination ? undefined : retarget(value, relative(to, destination));
|
|
33
|
-
}
|
|
34
|
-
function edited(document, path, changes) {
|
|
35
|
-
let next = document;
|
|
36
|
-
for (const change of changes) {
|
|
37
|
-
if (change.path !== path)
|
|
21
|
+
function repointed(document, definition, from, to, moves, settled) {
|
|
22
|
+
const parsed = parse(document.content, from);
|
|
23
|
+
if (!parsed.ok)
|
|
24
|
+
return [];
|
|
25
|
+
const changes = [];
|
|
26
|
+
const names = new Set();
|
|
27
|
+
for (const { pointer, value } of definition === undefined
|
|
28
|
+
? []
|
|
29
|
+
: references(document, definition)) {
|
|
30
|
+
const [name = ''] = pointerSegments(pointer);
|
|
31
|
+
const named = target(from, value);
|
|
32
|
+
if (named === null || settled.has(`${from} ${name}`))
|
|
38
33
|
continue;
|
|
39
|
-
|
|
34
|
+
const destination = moves.get(named) ?? named;
|
|
35
|
+
if (target(to, value) === destination)
|
|
40
36
|
continue;
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
37
|
+
changes.push({
|
|
38
|
+
kind: 'reference',
|
|
39
|
+
pointer,
|
|
40
|
+
value: retarget(value, relative(to, destination)),
|
|
41
|
+
prior: value,
|
|
42
|
+
});
|
|
43
|
+
names.add(name);
|
|
44
|
+
}
|
|
45
|
+
if (changes.length === 0)
|
|
46
|
+
return [];
|
|
47
|
+
const applied = apply(parsed.document, changes);
|
|
48
|
+
if (applied.outcome !== 'applied')
|
|
49
|
+
return [];
|
|
50
|
+
const next = applied.document;
|
|
51
|
+
return [...names].map((name) => ({ kind: 'field', path: from, name, value: next.fields[name] }));
|
|
52
|
+
}
|
|
53
|
+
function edited(document, changes) {
|
|
54
|
+
const parts = new Map();
|
|
55
|
+
for (const change of changes) {
|
|
56
|
+
if (change.kind === 'preamble')
|
|
57
|
+
parts.set('preamble', { kind: 'preamble', value: change.value, prior: document.preamble });
|
|
58
|
+
if (change.kind === 'section')
|
|
59
|
+
parts.set(`section ${change.name}`, {
|
|
44
60
|
kind: 'section',
|
|
45
61
|
name: change.name,
|
|
46
62
|
value: change.value,
|
|
47
|
-
prior:
|
|
63
|
+
prior: document.sections[change.name] ?? '',
|
|
48
64
|
});
|
|
49
|
-
if (applied.outcome === 'applied')
|
|
50
|
-
next = applied.document;
|
|
51
65
|
}
|
|
52
|
-
|
|
66
|
+
if (parts.size === 0)
|
|
67
|
+
return document;
|
|
68
|
+
const applied = apply(document, [...parts.values()]);
|
|
69
|
+
return applied.outcome === 'applied' ? applied.document : document;
|
|
53
70
|
}
|
|
54
71
|
function bodies(path, before, after) {
|
|
55
72
|
const changes = [];
|
|
@@ -64,7 +81,7 @@ function bodies(path, before, after) {
|
|
|
64
81
|
return changes;
|
|
65
82
|
}
|
|
66
83
|
function relinks(path, moved, before, moves) {
|
|
67
|
-
|
|
84
|
+
const changes = [];
|
|
68
85
|
for (const link of links(before, path)) {
|
|
69
86
|
const destination = moves.get(link.target) ?? link.target;
|
|
70
87
|
if (destination === link.target && moved === path)
|
|
@@ -72,19 +89,23 @@ function relinks(path, moved, before, moves) {
|
|
|
72
89
|
const to = relative(moved, destination);
|
|
73
90
|
if (to === relative(path, link.target))
|
|
74
91
|
continue;
|
|
75
|
-
|
|
76
|
-
if (applied.outcome === 'refuse') {
|
|
77
|
-
return {
|
|
78
|
-
kind: 'refused',
|
|
79
|
-
refusal: {
|
|
80
|
-
rule: applied.refusal.rule,
|
|
81
|
-
file: path,
|
|
82
|
-
...(applied.refusal.location === undefined ? {} : { location: applied.refusal.location }),
|
|
83
|
-
},
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
after = applied.document;
|
|
92
|
+
changes.push({ kind: 'link', href: link.href, to });
|
|
87
93
|
}
|
|
94
|
+
if (changes.length === 0)
|
|
95
|
+
return { kind: 'changes', changes: [] };
|
|
96
|
+
const applied = apply(before, changes);
|
|
97
|
+
if (applied.outcome === 'refuse') {
|
|
98
|
+
return {
|
|
99
|
+
kind: 'refused',
|
|
100
|
+
refusal: {
|
|
101
|
+
code: applied.refusal.rule,
|
|
102
|
+
retryable: false,
|
|
103
|
+
file: path,
|
|
104
|
+
...(applied.refusal.location === undefined ? {} : { location: applied.refusal.location }),
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
const after = applied.document;
|
|
88
109
|
if (after === before)
|
|
89
110
|
return { kind: 'changes', changes: [] };
|
|
90
111
|
if (before.type === undefined) {
|
|
@@ -92,93 +113,93 @@ function relinks(path, moved, before, moves) {
|
|
|
92
113
|
}
|
|
93
114
|
return { kind: 'changes', changes: bodies(path, before, after) };
|
|
94
115
|
}
|
|
95
|
-
function
|
|
96
|
-
|
|
97
|
-
const item = merged[index];
|
|
98
|
-
if (item.path !== path)
|
|
99
|
-
continue;
|
|
100
|
-
if (item.kind === 'section' || item.kind === 'preamble')
|
|
101
|
-
merged.splice(index, 1);
|
|
102
|
-
}
|
|
116
|
+
function partKey(change) {
|
|
117
|
+
return change.kind === 'section' ? `section ${change.name}` : change.kind;
|
|
103
118
|
}
|
|
104
|
-
function rewrites(changes, visible) {
|
|
119
|
+
function* rewrites(changes, visible) {
|
|
105
120
|
const moves = new Map();
|
|
106
121
|
const settled = new Set();
|
|
107
|
-
|
|
122
|
+
const parts = new Map();
|
|
123
|
+
for (const [index, change] of changes.entries()) {
|
|
108
124
|
if (change.kind === 'move')
|
|
109
125
|
moves.set(change.path, change.to);
|
|
110
|
-
if (change.kind === 'remove' ||
|
|
126
|
+
if (change.kind === 'remove' ||
|
|
127
|
+
change.kind === 'replace' ||
|
|
128
|
+
change.kind === 'create' ||
|
|
129
|
+
change.kind === 'binary') {
|
|
111
130
|
settled.add(change.path);
|
|
112
131
|
}
|
|
113
132
|
if (change.kind === 'field')
|
|
114
133
|
settled.add(`${change.path} ${change.name}`);
|
|
134
|
+
if (isPart(change)) {
|
|
135
|
+
const held = parts.get(change.path);
|
|
136
|
+
if (held === undefined)
|
|
137
|
+
parts.set(change.path, [index]);
|
|
138
|
+
else
|
|
139
|
+
held.push(index);
|
|
140
|
+
}
|
|
115
141
|
}
|
|
116
142
|
if (moves.size === 0)
|
|
117
143
|
return { kind: 'changes', changes };
|
|
118
144
|
const merged = [...changes];
|
|
119
145
|
const added = [];
|
|
120
|
-
for (const [path, document] of
|
|
146
|
+
for (const [path, document] of documentsOf(visible.collection)) {
|
|
121
147
|
if (settled.has(path))
|
|
122
148
|
continue;
|
|
123
149
|
const moved = moves.get(path) ?? path;
|
|
124
150
|
const definition = definitionOf(governing(visible.collection.composition, path), document.type);
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
const value = repointed(document.fields[name], path, moved, moves);
|
|
129
|
-
if (value === undefined)
|
|
130
|
-
continue;
|
|
131
|
-
added.push({ kind: 'field', path, name, value });
|
|
132
|
-
}
|
|
133
|
-
const relinked = relinks(path, moved, edited(document, path, merged), moves);
|
|
151
|
+
added.push(...repointed(document, definition, path, moved, moves, settled));
|
|
152
|
+
const own = parts.get(path) ?? [];
|
|
153
|
+
const relinked = relinks(path, moved, edited(document, own.map((index) => merged[index])), moves);
|
|
134
154
|
if (relinked.kind === 'refused')
|
|
135
155
|
return relinked;
|
|
156
|
+
const keyed = new Map();
|
|
157
|
+
for (const index of own) {
|
|
158
|
+
const key = partKey(merged[index]);
|
|
159
|
+
if (!keyed.has(key))
|
|
160
|
+
keyed.set(key, index);
|
|
161
|
+
}
|
|
136
162
|
for (const change of relinked.changes) {
|
|
137
163
|
if (change.kind === 'replace') {
|
|
138
|
-
|
|
164
|
+
for (const index of own) {
|
|
165
|
+
const kind = merged[index]?.kind;
|
|
166
|
+
if (kind === 'section' || kind === 'preamble')
|
|
167
|
+
merged[index] = null;
|
|
168
|
+
}
|
|
139
169
|
added.push(change);
|
|
140
170
|
continue;
|
|
141
171
|
}
|
|
142
|
-
const index =
|
|
143
|
-
|
|
144
|
-
(change.kind !== 'section' || (item.kind === 'section' && item.name === change.name)));
|
|
145
|
-
if (index === -1)
|
|
172
|
+
const index = keyed.get(partKey(change));
|
|
173
|
+
if (index === undefined)
|
|
146
174
|
added.push(change);
|
|
147
175
|
else
|
|
148
176
|
merged[index] = change;
|
|
149
177
|
}
|
|
178
|
+
yield ROW_WEIGHT + visible.collection.text[path].length;
|
|
150
179
|
}
|
|
151
|
-
return {
|
|
180
|
+
return {
|
|
181
|
+
kind: 'changes',
|
|
182
|
+
changes: [...added, ...merged.filter((change) => change !== null)],
|
|
183
|
+
};
|
|
152
184
|
}
|
|
153
|
-
function anticipated(changes, preconditions, rewritten, visible) {
|
|
154
|
-
const
|
|
185
|
+
function* anticipated(changes, preconditions, rewritten, visible) {
|
|
186
|
+
const given = new Set(changes);
|
|
187
|
+
const derived = rewritten.filter((change) => !given.has(change));
|
|
155
188
|
return {
|
|
156
189
|
changes: rewritten,
|
|
157
|
-
preconditions: [...preconditionsOf(derived, visible, []), ...preconditions],
|
|
190
|
+
preconditions: [...(yield* preconditionsOf(derived, visible, [])), ...preconditions],
|
|
158
191
|
};
|
|
159
192
|
}
|
|
160
|
-
export function providerRewritten(batch, visible) {
|
|
161
|
-
const rewritten = rewrites(batch.changes, visible);
|
|
193
|
+
export function* providerRewritten(batch, visible) {
|
|
194
|
+
const rewritten = yield* rewrites(batch.changes, visible);
|
|
162
195
|
if (rewritten.kind === 'refused')
|
|
163
196
|
return batch;
|
|
164
197
|
return {
|
|
165
198
|
...batch,
|
|
166
|
-
...anticipated(batch.changes, batch.preconditions, rewritten.changes, visible),
|
|
199
|
+
...(yield* anticipated(batch.changes, batch.preconditions, rewritten.changes, visible)),
|
|
167
200
|
};
|
|
168
201
|
}
|
|
169
|
-
function
|
|
170
|
-
const values = Array.isArray(value) ? value : [value];
|
|
171
|
-
const resolved = [];
|
|
172
|
-
for (const item of values) {
|
|
173
|
-
if (typeof item !== 'string')
|
|
174
|
-
continue;
|
|
175
|
-
const named = target(from, item);
|
|
176
|
-
if (named !== null)
|
|
177
|
-
resolved.push(named);
|
|
178
|
-
}
|
|
179
|
-
return resolved;
|
|
180
|
-
}
|
|
181
|
-
function incoming(collection, removed) {
|
|
202
|
+
function* incoming(collection, removed, candidates) {
|
|
182
203
|
const found = new Map();
|
|
183
204
|
const note = (target, file) => {
|
|
184
205
|
if (!removed.has(target))
|
|
@@ -187,16 +208,23 @@ function incoming(collection, removed) {
|
|
|
187
208
|
referrers.add(file);
|
|
188
209
|
found.set(target, referrers);
|
|
189
210
|
};
|
|
190
|
-
|
|
211
|
+
const read = function* (paths) {
|
|
212
|
+
for (const path of [...new Set(paths)].sort()) {
|
|
213
|
+
const document = collection.document(path);
|
|
214
|
+
if (document !== undefined)
|
|
215
|
+
yield [path, document];
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
for (const [path, document] of candidates === null ? documentsOf(collection) : read(candidates)) {
|
|
191
219
|
const definition = definitionOf(governing(collection.composition, path), document.type);
|
|
192
|
-
for (const
|
|
193
|
-
if (
|
|
220
|
+
for (const { value, to } of definition === undefined ? [] : references(document, definition)) {
|
|
221
|
+
if (to.includes(ATTACHMENT))
|
|
194
222
|
continue;
|
|
195
|
-
|
|
223
|
+
const named = target(path, value);
|
|
224
|
+
if (named !== null)
|
|
196
225
|
note(named, path);
|
|
197
226
|
}
|
|
198
|
-
|
|
199
|
-
note(link.target, path);
|
|
227
|
+
yield ROW_WEIGHT + collection.text[path].length;
|
|
200
228
|
}
|
|
201
229
|
const [first] = [...found.keys()].sort();
|
|
202
230
|
if (first === undefined)
|
|
@@ -207,18 +235,26 @@ function incoming(collection, removed) {
|
|
|
207
235
|
every.add(file);
|
|
208
236
|
return {
|
|
209
237
|
kind: 'refused',
|
|
210
|
-
refusal: {
|
|
238
|
+
refusal: {
|
|
239
|
+
code: 'referenced-missing-file',
|
|
240
|
+
retryable: false,
|
|
241
|
+
file: first,
|
|
242
|
+
referrers: [...every].sort(),
|
|
243
|
+
},
|
|
211
244
|
};
|
|
212
245
|
}
|
|
213
246
|
function affected(change) {
|
|
214
247
|
return change.kind === 'move' ? [change.path, change.to] : [change.path];
|
|
215
248
|
}
|
|
216
249
|
function guard(changes, input) {
|
|
217
|
-
const
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
250
|
+
const overridden = new Map();
|
|
251
|
+
const existing = {
|
|
252
|
+
has: (path) => overridden.get(path) ??
|
|
253
|
+
(Object.hasOwn(input.visible.documents, path) || Object.hasOwn(input.visible.binaries, path)),
|
|
254
|
+
add: (path) => void overridden.set(path, true),
|
|
255
|
+
delete: (path) => void overridden.set(path, false),
|
|
256
|
+
};
|
|
257
|
+
for (const [index, change] of changes.entries()) {
|
|
222
258
|
for (const path of affected(change)) {
|
|
223
259
|
const normalized = normalize(path);
|
|
224
260
|
if (normalized === null || normalized === '')
|
|
@@ -227,7 +263,7 @@ function guard(changes, input) {
|
|
|
227
263
|
return refuse('provider-owned-path', normalized);
|
|
228
264
|
}
|
|
229
265
|
const operation = change.kind === 'remove' ? 'delete' : 'write';
|
|
230
|
-
if (!covered(input.operations[operation], normalized))
|
|
266
|
+
if (!input.local && !covered(input.operations[operation], normalized))
|
|
231
267
|
return refuse(`${operation}-grant`, normalized);
|
|
232
268
|
}
|
|
233
269
|
if (change.kind === 'create') {
|
|
@@ -236,12 +272,26 @@ function guard(changes, input) {
|
|
|
236
272
|
existing.add(change.path);
|
|
237
273
|
continue;
|
|
238
274
|
}
|
|
275
|
+
if (change.kind === 'binary') {
|
|
276
|
+
if (input.visible.documents[change.path] !== undefined ||
|
|
277
|
+
(existing.has(change.path) &&
|
|
278
|
+
input.bases.some((base) => base.kind === 'absent' && base.path === change.path))) {
|
|
279
|
+
return refuse('path-collision', change.path);
|
|
280
|
+
}
|
|
281
|
+
if (!input.local &&
|
|
282
|
+
existing.has(change.path) &&
|
|
283
|
+
input.visible.revisions[departure(changes, index)] === undefined) {
|
|
284
|
+
return refuse('unknown-revision', change.path);
|
|
285
|
+
}
|
|
286
|
+
existing.add(change.path);
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
239
289
|
if (change.kind === 'move') {
|
|
240
290
|
if (!existing.has(change.path))
|
|
241
291
|
return refuse('path-absent', change.path);
|
|
242
292
|
if (existing.has(change.to))
|
|
243
293
|
return refuse('path-collision', change.to);
|
|
244
|
-
if (
|
|
294
|
+
if (unrevised(input, change.path)) {
|
|
245
295
|
return refuse('unknown-revision', change.path);
|
|
246
296
|
}
|
|
247
297
|
existing.delete(change.path);
|
|
@@ -251,30 +301,53 @@ function guard(changes, input) {
|
|
|
251
301
|
if (!existing.has(change.path))
|
|
252
302
|
return refuse('path-absent', change.path);
|
|
253
303
|
if (change.kind === 'remove') {
|
|
254
|
-
if (
|
|
304
|
+
if (unrevised(input, change.path)) {
|
|
255
305
|
return refuse('unknown-revision', change.path);
|
|
256
306
|
}
|
|
257
307
|
existing.delete(change.path);
|
|
258
308
|
continue;
|
|
259
309
|
}
|
|
260
310
|
if (change.kind === 'replace') {
|
|
261
|
-
if (input
|
|
311
|
+
if (unrevised(input, change.path)) {
|
|
262
312
|
return refuse('unknown-revision', change.path);
|
|
263
313
|
}
|
|
264
314
|
continue;
|
|
265
315
|
}
|
|
266
|
-
if (input.visible.collection.
|
|
316
|
+
if (input.visible.collection.document(change.path) === undefined) {
|
|
267
317
|
return refuse('path-absent', change.path);
|
|
268
318
|
}
|
|
269
319
|
}
|
|
270
320
|
return null;
|
|
271
321
|
}
|
|
272
|
-
function
|
|
273
|
-
return visible.revisions[path] === undefined
|
|
322
|
+
function unrevised(input, path) {
|
|
323
|
+
return (!input.local && input.queued?.has(path) !== true && input.visible.revisions[path] === undefined);
|
|
324
|
+
}
|
|
325
|
+
export function queuedDocuments(queue) {
|
|
326
|
+
const paths = new Set();
|
|
327
|
+
for (const batch of queue) {
|
|
328
|
+
for (const change of batch.changes) {
|
|
329
|
+
if (change.kind === 'binary')
|
|
330
|
+
continue;
|
|
331
|
+
if (change.kind === 'remove')
|
|
332
|
+
paths.delete(change.path);
|
|
333
|
+
else if (change.kind !== 'move')
|
|
334
|
+
paths.add(change.path);
|
|
335
|
+
else if (paths.delete(change.path))
|
|
336
|
+
paths.add(change.to);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
return paths;
|
|
340
|
+
}
|
|
341
|
+
function identity(precondition) {
|
|
342
|
+
const part = precondition.kind === 'field' || precondition.kind === 'section' ? precondition.name : '';
|
|
343
|
+
return `${precondition.kind} ${precondition.path} ${part}`;
|
|
274
344
|
}
|
|
275
345
|
function existence(visible, path) {
|
|
276
346
|
const revision = visible.revisions[path];
|
|
277
|
-
|
|
347
|
+
if (revision !== undefined)
|
|
348
|
+
return { kind: 'revision', path, revision };
|
|
349
|
+
const content = visible.documents[path];
|
|
350
|
+
return content === undefined ? { kind: 'present', path } : { kind: 'content', path, content };
|
|
278
351
|
}
|
|
279
352
|
function sameTarget(precondition, change) {
|
|
280
353
|
if (precondition.kind !== change.kind || precondition.path !== change.path)
|
|
@@ -282,25 +355,25 @@ function sameTarget(precondition, change) {
|
|
|
282
355
|
return ((precondition.kind !== 'field' && precondition.kind !== 'section') ||
|
|
283
356
|
((change.kind === 'field' || change.kind === 'section') && precondition.name === change.name));
|
|
284
357
|
}
|
|
285
|
-
function preconditionsOf(changes, visible, bases) {
|
|
358
|
+
function* preconditionsOf(changes, visible, bases) {
|
|
286
359
|
const preconditions = [];
|
|
287
360
|
const seen = new Set();
|
|
288
361
|
const add = (precondition) => {
|
|
289
|
-
const
|
|
290
|
-
|
|
291
|
-
if (seen.has(identity))
|
|
362
|
+
const held = identity(precondition);
|
|
363
|
+
if (seen.has(held))
|
|
292
364
|
return;
|
|
293
|
-
seen.add(
|
|
365
|
+
seen.add(held);
|
|
294
366
|
preconditions.push(precondition);
|
|
295
367
|
};
|
|
296
368
|
for (const change of changes) {
|
|
369
|
+
yield ROW_WEIGHT;
|
|
297
370
|
if (change.kind === 'field' || change.kind === 'section' || change.kind === 'preamble') {
|
|
298
371
|
const based = bases.find((base) => sameTarget(base, change));
|
|
299
372
|
if (based !== undefined) {
|
|
300
373
|
add(based);
|
|
301
374
|
continue;
|
|
302
375
|
}
|
|
303
|
-
const document = visible.collection.
|
|
376
|
+
const document = visible.collection.document(change.path);
|
|
304
377
|
if (change.kind === 'field') {
|
|
305
378
|
add({
|
|
306
379
|
kind: 'field',
|
|
@@ -325,6 +398,13 @@ function preconditionsOf(changes, visible, bases) {
|
|
|
325
398
|
add({ kind: 'absent', path: change.path });
|
|
326
399
|
continue;
|
|
327
400
|
}
|
|
401
|
+
if (change.kind === 'binary') {
|
|
402
|
+
const revision = visible.revisions[change.path];
|
|
403
|
+
add(visible.binaries[change.path] === undefined || revision === undefined
|
|
404
|
+
? { kind: 'absent', path: change.path }
|
|
405
|
+
: { kind: 'revision', path: change.path, revision });
|
|
406
|
+
continue;
|
|
407
|
+
}
|
|
328
408
|
add(existence(visible, change.path));
|
|
329
409
|
if (change.kind === 'move')
|
|
330
410
|
add({ kind: 'absent', path: change.to });
|
|
@@ -334,10 +414,10 @@ function preconditionsOf(changes, visible, bases) {
|
|
|
334
414
|
function depth(path) {
|
|
335
415
|
return path.split('/').length;
|
|
336
416
|
}
|
|
337
|
-
function
|
|
338
|
-
const written =
|
|
417
|
+
function* exceeding(limits, changes, effective, resulting) {
|
|
418
|
+
const written = effective.map((change) => (change.kind === 'move' ? change.to : change.path));
|
|
339
419
|
const created = changes
|
|
340
|
-
.filter((change) => change.kind === 'create' || change.kind === 'move')
|
|
420
|
+
.filter((change) => change.kind === 'create' || change.kind === 'move' || change.kind === 'binary')
|
|
341
421
|
.map((change) => (change.kind === 'move' ? change.to : change.path));
|
|
342
422
|
for (const limit of limits) {
|
|
343
423
|
const value = limit.value;
|
|
@@ -351,9 +431,13 @@ function exceeded(limits, changes, resulting) {
|
|
|
351
431
|
return refuse('provider-limit', undefined, limit.name);
|
|
352
432
|
}
|
|
353
433
|
if (limit.name === 'document-size') {
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
434
|
+
for (const path of written) {
|
|
435
|
+
const text = resulting.documents[path] ?? '';
|
|
436
|
+
if (new TextEncoder().encode(text).byteLength > value) {
|
|
437
|
+
return refuse('provider-limit', path, limit.name);
|
|
438
|
+
}
|
|
439
|
+
yield ROW_WEIGHT + text.length;
|
|
440
|
+
}
|
|
357
441
|
}
|
|
358
442
|
if (limit.name === 'path-length') {
|
|
359
443
|
const long = created.find((path) => path.length > value);
|
|
@@ -368,32 +452,389 @@ function exceeded(limits, changes, resulting) {
|
|
|
368
452
|
}
|
|
369
453
|
return null;
|
|
370
454
|
}
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
455
|
+
function shellRelative(text, path, handle, definition) {
|
|
456
|
+
if (!text.includes(`/rooms/${handle}/`))
|
|
457
|
+
return null;
|
|
458
|
+
const parsed = parse(text, path);
|
|
459
|
+
if (!parsed.ok)
|
|
460
|
+
return null;
|
|
461
|
+
const result = relativize(parsed.document, path, handle, definition(parsed.document.type));
|
|
462
|
+
return result.outcome === 'applied' && result.document.content !== text ? result.document : null;
|
|
463
|
+
}
|
|
464
|
+
function relativeField(path, name, value, handle, definition) {
|
|
465
|
+
const opened = parse('---\n---\n');
|
|
466
|
+
if (definition === undefined ||
|
|
467
|
+
!opened.ok ||
|
|
468
|
+
!JSON.stringify(value ?? null).includes(`/rooms/${handle}/`))
|
|
469
|
+
return value;
|
|
470
|
+
const held = apply(opened.document, [{ kind: 'field', name, value, prior: undefined }]);
|
|
471
|
+
if (held.outcome !== 'applied')
|
|
472
|
+
return value;
|
|
473
|
+
const result = relativize(held.document, path, handle, definition);
|
|
474
|
+
return result.outcome === 'applied' ? result.document.fields[name] : value;
|
|
475
|
+
}
|
|
476
|
+
function relativeChanges(input) {
|
|
477
|
+
const { handle, composition, visible } = input;
|
|
478
|
+
const typed = (path) => (type) => definitionOf(governing(composition, path), type);
|
|
479
|
+
const body = (path, text) => shellRelative(text, path, handle, () => undefined)?.content ?? text;
|
|
480
|
+
return input.changes.map((change) => {
|
|
481
|
+
const definition = typed(change.path);
|
|
482
|
+
if (change.kind === 'field') {
|
|
483
|
+
const type = visible.collection.document(change.path)?.type;
|
|
484
|
+
const value = relativeField(change.path, change.name, change.value, handle, definition(type));
|
|
485
|
+
return value === change.value ? change : { ...change, value };
|
|
486
|
+
}
|
|
487
|
+
if (change.kind === 'section' || change.kind === 'preamble') {
|
|
488
|
+
const value = body(change.path, change.value);
|
|
489
|
+
return value === change.value ? change : { ...change, value };
|
|
490
|
+
}
|
|
491
|
+
if (change.kind === 'replace' || (change.kind === 'create' && 'content' in change)) {
|
|
492
|
+
const content = shellRelative(change.content, change.path, handle, definition)?.content ?? change.content;
|
|
493
|
+
return content === change.content ? change : { ...change, content };
|
|
494
|
+
}
|
|
495
|
+
if (change.kind !== 'create')
|
|
496
|
+
return change;
|
|
497
|
+
const type = typeof change.fields.type === 'string' ? change.fields.type : undefined;
|
|
498
|
+
const fields = Object.fromEntries(Object.entries(change.fields).map(([name, value]) => [
|
|
499
|
+
name,
|
|
500
|
+
relativeField(change.path, name, value, handle, definition(type)),
|
|
501
|
+
]));
|
|
502
|
+
const sections = Object.fromEntries(Object.entries(change.sections).map(([name, value]) => [name, body(change.path, value)]));
|
|
503
|
+
return { ...change, fields, sections };
|
|
504
|
+
});
|
|
505
|
+
}
|
|
506
|
+
function* unmodeledType(collection, composition) {
|
|
507
|
+
if (available(composition))
|
|
508
|
+
return false;
|
|
509
|
+
for (const [path, document] of documentsOf(collection)) {
|
|
510
|
+
if (document.type !== undefined &&
|
|
375
511
|
document.type !== 'any' &&
|
|
376
|
-
governing(
|
|
512
|
+
governing(composition, path).status === 'unavailable')
|
|
513
|
+
return true;
|
|
514
|
+
yield ROW_WEIGHT + collection.text[path].length;
|
|
515
|
+
}
|
|
516
|
+
return false;
|
|
517
|
+
}
|
|
518
|
+
function unqueueable(file) {
|
|
519
|
+
return { kind: 'refused', refusal: { code: 'unknown-revision', retryable: false, file } };
|
|
520
|
+
}
|
|
521
|
+
function isPart(change) {
|
|
522
|
+
return change.kind === 'field' || change.kind === 'section' || change.kind === 'preamble';
|
|
523
|
+
}
|
|
524
|
+
function touched(change) {
|
|
525
|
+
return change.kind === 'move' ? [change.path, change.to] : [change.path];
|
|
526
|
+
}
|
|
527
|
+
function partsApplied(content, path, changes) {
|
|
528
|
+
const parsed = parse(content, path);
|
|
529
|
+
if (!parsed.ok)
|
|
530
|
+
return null;
|
|
531
|
+
const document = parsed.document;
|
|
532
|
+
const parts = new Map();
|
|
533
|
+
for (const change of changes) {
|
|
534
|
+
if (change.kind === 'field')
|
|
535
|
+
parts.set(`field ${change.name}`, {
|
|
536
|
+
kind: 'field',
|
|
537
|
+
name: change.name,
|
|
538
|
+
value: change.value,
|
|
539
|
+
prior: document.fields[change.name],
|
|
540
|
+
});
|
|
541
|
+
else if (change.kind === 'section')
|
|
542
|
+
parts.set(`section ${change.name}`, {
|
|
543
|
+
kind: 'section',
|
|
544
|
+
name: change.name,
|
|
545
|
+
value: change.value,
|
|
546
|
+
prior: document.sections[change.name] ?? '',
|
|
547
|
+
});
|
|
548
|
+
else
|
|
549
|
+
parts.set('preamble', { kind: 'preamble', value: change.value, prior: document.preamble });
|
|
550
|
+
}
|
|
551
|
+
const applied = apply(document, [...parts.values()]);
|
|
552
|
+
return applied.outcome === 'applied' ? serialize(applied.document) : null;
|
|
553
|
+
}
|
|
554
|
+
function settledParts(changes, deferred) {
|
|
555
|
+
for (const [held, parts] of deferred) {
|
|
556
|
+
const content = partsApplied(held.content, held.path, parts);
|
|
557
|
+
if (content === null)
|
|
558
|
+
return held.path;
|
|
559
|
+
changes[changes.indexOf(held)] = { ...held, content };
|
|
560
|
+
}
|
|
561
|
+
deferred.clear();
|
|
562
|
+
return null;
|
|
563
|
+
}
|
|
564
|
+
function folded(changes, change, appending, deferred) {
|
|
565
|
+
const held = changes.find((item) => item.path === change.path && (item.kind === 'create' || item.kind === 'replace'));
|
|
566
|
+
const whole = held === undefined ? -1 : changes.indexOf(held);
|
|
567
|
+
const occupied = changes.some((item) => !isPart(item) && item !== held && touched(item).includes(change.path));
|
|
568
|
+
if (isPart(change)) {
|
|
569
|
+
const same = changes.findIndex((item) => item.kind === change.kind &&
|
|
570
|
+
item.path === change.path &&
|
|
571
|
+
(change.kind === 'preamble' || ('name' in item && item.name === change.name)));
|
|
572
|
+
if (same !== -1) {
|
|
573
|
+
changes[same] = change;
|
|
574
|
+
return 'folded';
|
|
575
|
+
}
|
|
576
|
+
if (held === undefined) {
|
|
577
|
+
if (!appending)
|
|
578
|
+
return 'skipped';
|
|
579
|
+
if (occupied)
|
|
580
|
+
return 'refused';
|
|
581
|
+
changes.push(change);
|
|
582
|
+
return 'appended';
|
|
583
|
+
}
|
|
584
|
+
if ('content' in held) {
|
|
585
|
+
const parts = deferred.get(held);
|
|
586
|
+
if (parts === undefined)
|
|
587
|
+
deferred.set(held, [change]);
|
|
588
|
+
else
|
|
589
|
+
parts.push(change);
|
|
590
|
+
return 'folded';
|
|
591
|
+
}
|
|
592
|
+
if (change.kind === 'preamble')
|
|
593
|
+
return 'refused';
|
|
594
|
+
changes[whole] =
|
|
595
|
+
change.kind === 'field'
|
|
596
|
+
? { ...held, fields: { ...held.fields, [change.name]: change.value } }
|
|
597
|
+
: { ...held, sections: { ...held.sections, [change.name]: change.value } };
|
|
598
|
+
return 'folded';
|
|
599
|
+
}
|
|
600
|
+
if (change.kind === 'replace') {
|
|
601
|
+
if (held !== undefined) {
|
|
602
|
+
if (!('content' in held))
|
|
603
|
+
return 'refused';
|
|
604
|
+
changes[whole] = { ...held, content: change.content };
|
|
605
|
+
return 'folded';
|
|
606
|
+
}
|
|
607
|
+
const parts = changes.filter((item) => isPart(item) && item.path === change.path);
|
|
608
|
+
if (parts.length === 0 && !appending)
|
|
609
|
+
return 'skipped';
|
|
610
|
+
if (occupied)
|
|
611
|
+
return 'refused';
|
|
612
|
+
changes.splice(0, changes.length, ...changes.filter((item) => !parts.includes(item)), change);
|
|
613
|
+
return 'appended';
|
|
614
|
+
}
|
|
615
|
+
if (changes.some((item) => touched(change).some((path) => touched(item).includes(path))))
|
|
616
|
+
return 'refused';
|
|
617
|
+
changes.push(change);
|
|
618
|
+
return 'appended';
|
|
619
|
+
}
|
|
620
|
+
function relinked(batch, move, before, after, appending) {
|
|
621
|
+
const derived = finished(rewrites([move], after));
|
|
622
|
+
if (derived.kind === 'refused')
|
|
623
|
+
return derived;
|
|
624
|
+
const changes = [...batch.changes];
|
|
625
|
+
const appended = [];
|
|
626
|
+
let changed = false;
|
|
627
|
+
const deferred = new Map();
|
|
628
|
+
for (const change of derived.changes) {
|
|
629
|
+
if (change === move)
|
|
630
|
+
continue;
|
|
631
|
+
const unsettled = isPart(change) ? null : settledParts(changes, deferred);
|
|
632
|
+
if (unsettled !== null)
|
|
633
|
+
return unqueueable(unsettled);
|
|
634
|
+
const outcome = folded(changes, change, appending, deferred);
|
|
635
|
+
if (outcome === 'refused')
|
|
636
|
+
return unqueueable(change.path);
|
|
637
|
+
if (outcome === 'appended')
|
|
638
|
+
appended.push(change);
|
|
639
|
+
if (outcome !== 'skipped')
|
|
640
|
+
changed = true;
|
|
641
|
+
}
|
|
642
|
+
const unsettled = settledParts(changes, deferred);
|
|
643
|
+
if (unsettled !== null)
|
|
644
|
+
return unqueueable(unsettled);
|
|
645
|
+
if (!changed)
|
|
646
|
+
return { kind: 'relinked', batch };
|
|
647
|
+
const held = new Set(batch.preconditions.map(identity));
|
|
648
|
+
return {
|
|
649
|
+
kind: 'relinked',
|
|
650
|
+
batch: immutable({
|
|
651
|
+
...batch,
|
|
652
|
+
id: changeBatchIdentity(),
|
|
653
|
+
changes,
|
|
654
|
+
preconditions: [
|
|
655
|
+
...batch.preconditions,
|
|
656
|
+
...finished(preconditionsOf(appended, before, [])).filter((precondition) => !held.has(identity(precondition))),
|
|
657
|
+
],
|
|
658
|
+
}),
|
|
659
|
+
};
|
|
660
|
+
}
|
|
661
|
+
function leading(changes, at) {
|
|
662
|
+
const from = departure(changes, at);
|
|
663
|
+
return new Set(changes.filter((change, index) => index < at && change.kind === 'move' && departure(changes, index) === from));
|
|
664
|
+
}
|
|
665
|
+
function created(batch, at) {
|
|
666
|
+
const file = batch.changes[at];
|
|
667
|
+
return (leading(batch.changes, at).size === 0 &&
|
|
668
|
+
batch.preconditions.some((precondition) => precondition.kind === 'absent' && precondition.path === file.path));
|
|
669
|
+
}
|
|
670
|
+
function withdrawnFile(batch, change, at) {
|
|
671
|
+
const file = batch.changes[at];
|
|
672
|
+
const from = departure(batch.changes, at);
|
|
673
|
+
const moves = leading(batch.changes, at);
|
|
674
|
+
const fresh = created(batch, at);
|
|
675
|
+
const vacated = new Set([...moves].map((move) => move.to));
|
|
676
|
+
if (fresh)
|
|
677
|
+
vacated.add(file.path);
|
|
678
|
+
const replaced = fresh
|
|
679
|
+
? change.kind === 'remove'
|
|
680
|
+
? []
|
|
681
|
+
: [{ ...file, path: change.to }]
|
|
682
|
+
: change.kind === 'remove'
|
|
683
|
+
? [{ kind: 'remove', path: from }]
|
|
684
|
+
: change.to === from
|
|
685
|
+
? [{ ...file, path: from }]
|
|
686
|
+
: [
|
|
687
|
+
{ kind: 'move', path: from, to: change.to },
|
|
688
|
+
{ ...file, path: change.to },
|
|
689
|
+
];
|
|
690
|
+
const added = change.kind === 'move' && (fresh || change.to !== from)
|
|
691
|
+
? [{ kind: 'absent', path: change.to }]
|
|
692
|
+
: [];
|
|
693
|
+
const changes = batch.changes.flatMap((item) => item === file ? replaced : item.kind === 'move' && moves.has(item) ? [] : [item]);
|
|
694
|
+
if (changes.length === 0)
|
|
695
|
+
return null;
|
|
696
|
+
const preconditions = [
|
|
697
|
+
...added,
|
|
698
|
+
...batch.preconditions.filter((precondition) => precondition.kind !== 'absent' || !vacated.has(precondition.path)),
|
|
699
|
+
];
|
|
700
|
+
return immutable({ ...batch, id: changeBatchIdentity(), changes, preconditions });
|
|
701
|
+
}
|
|
702
|
+
export function requeued(queue, changes, visible, changeable, state) {
|
|
703
|
+
const next = [...queue];
|
|
704
|
+
const origins = new Map();
|
|
705
|
+
const withdrawn = new Map();
|
|
706
|
+
const owners = new Set();
|
|
707
|
+
const queued = new Set();
|
|
708
|
+
const kept = [];
|
|
709
|
+
const originOf = (batch) => origins.get(batch) ?? batch.id;
|
|
710
|
+
const open = (batch) => origins.has(batch) || changeable(batch);
|
|
711
|
+
for (const change of changes) {
|
|
712
|
+
const held = visible.binaries[change.path];
|
|
713
|
+
if ((change.kind !== 'move' && change.kind !== 'remove') || held?.state !== 'pending') {
|
|
714
|
+
kept.push(change);
|
|
715
|
+
continue;
|
|
716
|
+
}
|
|
717
|
+
let index = next.length - 1;
|
|
718
|
+
while (index >= 0 && queuedFile(next[index], change.path, held.localId) === -1)
|
|
719
|
+
index -= 1;
|
|
720
|
+
const owner = next[index];
|
|
721
|
+
if (owner === undefined || !open(owner))
|
|
722
|
+
return unqueueable(change.path);
|
|
723
|
+
const following = next.slice(index);
|
|
724
|
+
const rebuilt = [];
|
|
725
|
+
let before = change.kind === 'move' ? state(next.slice(0, index)) : visible;
|
|
726
|
+
for (const [offset, batch] of following.entries()) {
|
|
727
|
+
let current = batch;
|
|
728
|
+
if (change.kind === 'move') {
|
|
729
|
+
const after = state(next.slice(0, index + offset + 1));
|
|
730
|
+
const appending = offset === 0 && created(batch, queuedFile(batch, change.path, held.localId));
|
|
731
|
+
const relinking = relinked(batch, change, before, after, appending);
|
|
732
|
+
if (relinking.kind === 'refused')
|
|
733
|
+
return relinking;
|
|
734
|
+
current = relinking.batch;
|
|
735
|
+
before = after;
|
|
736
|
+
}
|
|
737
|
+
if (current !== batch && !open(batch))
|
|
738
|
+
return unqueueable(change.path);
|
|
739
|
+
rebuilt.push(offset === 0
|
|
740
|
+
? withdrawnFile(current, change, queuedFile(current, change.path, held.localId))
|
|
741
|
+
: current);
|
|
742
|
+
}
|
|
743
|
+
for (const [offset, batch] of following.entries()) {
|
|
744
|
+
const replaced = rebuilt[offset];
|
|
745
|
+
if (replaced === batch)
|
|
746
|
+
continue;
|
|
747
|
+
const origin = originOf(batch);
|
|
748
|
+
withdrawn.set(origin, replaced);
|
|
749
|
+
if (replaced !== null)
|
|
750
|
+
origins.set(replaced, origin);
|
|
751
|
+
}
|
|
752
|
+
owners.add(originOf(owner));
|
|
753
|
+
next.splice(index, following.length, ...rebuilt.filter((batch) => batch !== null));
|
|
754
|
+
queued.add(change.path);
|
|
755
|
+
}
|
|
756
|
+
return {
|
|
757
|
+
kind: 'requeued',
|
|
758
|
+
queue: withdrawn.size === 0 ? queue : next,
|
|
759
|
+
changes: kept,
|
|
760
|
+
queued,
|
|
761
|
+
withdrawn,
|
|
762
|
+
owners,
|
|
763
|
+
};
|
|
764
|
+
}
|
|
765
|
+
function covering(precondition) {
|
|
766
|
+
return precondition.kind === 'field' ||
|
|
767
|
+
precondition.kind === 'section' ||
|
|
768
|
+
precondition.kind === 'preamble'
|
|
769
|
+
? identity(precondition)
|
|
770
|
+
: `path ${precondition.path}`;
|
|
771
|
+
}
|
|
772
|
+
export function joined(queue, owner, extra) {
|
|
773
|
+
const later = new Set(queue.slice(queue.indexOf(owner) + 1).flatMap((batch) => batch.changes.flatMap(touched)));
|
|
774
|
+
const clash = extra.changes.flatMap(touched).find((path) => later.has(path));
|
|
775
|
+
if (clash !== undefined)
|
|
776
|
+
return unqueueable(clash);
|
|
777
|
+
const changes = [...owner.changes];
|
|
778
|
+
const deferred = new Map();
|
|
779
|
+
for (const change of extra.changes) {
|
|
780
|
+
const unsettled = isPart(change) ? null : settledParts(changes, deferred);
|
|
781
|
+
if (unsettled !== null)
|
|
782
|
+
return unqueueable(unsettled);
|
|
783
|
+
if (folded(changes, change, true, deferred) === 'refused')
|
|
784
|
+
return unqueueable(change.path);
|
|
785
|
+
}
|
|
786
|
+
const unsettled = settledParts(changes, deferred);
|
|
787
|
+
if (unsettled !== null)
|
|
788
|
+
return unqueueable(unsettled);
|
|
789
|
+
const held = new Set(owner.preconditions.map(covering));
|
|
790
|
+
const written = new Set(owner.changes.flatMap((change) => change.kind === 'create' || change.kind === 'replace' ? [change.path] : []));
|
|
791
|
+
const preconditions = [
|
|
792
|
+
...owner.preconditions,
|
|
793
|
+
...extra.preconditions.filter((precondition) => {
|
|
794
|
+
const key = covering(precondition);
|
|
795
|
+
return (!held.has(key) && (key === `path ${precondition.path}` || !written.has(precondition.path)));
|
|
796
|
+
}),
|
|
797
|
+
];
|
|
798
|
+
const batch = immutable({
|
|
799
|
+
...owner,
|
|
800
|
+
...(extra.validation === undefined ? {} : { validation: extra.validation }),
|
|
801
|
+
id: changeBatchIdentity(),
|
|
802
|
+
changes,
|
|
803
|
+
preconditions,
|
|
804
|
+
});
|
|
805
|
+
return { kind: 'joined', batch, queue: queue.map((item) => (item === owner ? batch : item)) };
|
|
806
|
+
}
|
|
807
|
+
export function limitedIn(queue, batch, limits, state) {
|
|
808
|
+
const at = queue.indexOf(batch);
|
|
809
|
+
const limited = finished(exceeding(limits, batch.changes, finished(providerRewritten(batch, state(queue.slice(0, at)))).changes, state(queue.slice(0, at + 1))));
|
|
810
|
+
return limited?.kind === 'refused' ? limited.refusal : null;
|
|
811
|
+
}
|
|
812
|
+
function* batching(input) {
|
|
813
|
+
const changes = relativeChanges(input);
|
|
814
|
+
const structural = changes.find((change) => change.kind === 'move' || change.kind === 'remove');
|
|
815
|
+
if (structural !== undefined &&
|
|
816
|
+
(yield* unmodeledType(input.visible.collection, input.composition)))
|
|
377
817
|
return refuse('model-unavailable', structural.path);
|
|
378
|
-
if (
|
|
818
|
+
if (changes.length === 0)
|
|
379
819
|
return refuse('no-change');
|
|
380
|
-
const rewritten = rewrites(
|
|
820
|
+
const rewritten = yield* rewrites(changes, input.visible);
|
|
381
821
|
if (rewritten.kind === 'refused')
|
|
382
822
|
return rewritten;
|
|
383
|
-
const changes = input.rewriting === 'provider' ? input.changes : rewritten.changes;
|
|
384
823
|
const refusal = guard(changes, input);
|
|
385
824
|
if (refusal !== null)
|
|
386
825
|
return refusal;
|
|
387
|
-
const preconditions = preconditionsOf(changes, input.visible, input.bases);
|
|
388
|
-
const predicted = input.
|
|
389
|
-
|
|
390
|
-
: { changes, preconditions };
|
|
391
|
-
const resulting = overlay(input.visible, [
|
|
826
|
+
const preconditions = yield* preconditionsOf(changes, input.visible, input.bases);
|
|
827
|
+
const predicted = yield* anticipated(changes, preconditions, rewritten.changes, input.visible);
|
|
828
|
+
const resulting = yield* overlaying(input.visible, [
|
|
392
829
|
{ id: '', baseRoomVersion: input.baseRoomVersion, ...predicted },
|
|
393
830
|
]);
|
|
831
|
+
const clean = input.clean !== undefined && available(input.composition) ? input.clean : null;
|
|
394
832
|
const removed = new Set(changes.filter((change) => change.kind === 'remove').map((change) => change.path));
|
|
395
833
|
if (removed.size > 0) {
|
|
396
|
-
const
|
|
834
|
+
const referrers = clean === null ? null : referring(clean, input.composition, removed);
|
|
835
|
+
const referenced = yield* incoming(collectionOf(resulting.documents, input.composition), removed, clean === null || referrers === null
|
|
836
|
+
? null
|
|
837
|
+
: [...referrers, ...(yield* differences(clean.text, resulting.documents))]);
|
|
397
838
|
if (referenced !== null)
|
|
398
839
|
return referenced;
|
|
399
840
|
}
|
|
@@ -403,27 +844,40 @@ export function buildBatch(input) {
|
|
|
403
844
|
const parsed = parse(content);
|
|
404
845
|
return parsed.ok && parsed.document.type !== undefined && parsed.document.type !== 'any';
|
|
405
846
|
};
|
|
406
|
-
const
|
|
847
|
+
const touched = [...new Set(predicted.changes.flatMap(affected))];
|
|
848
|
+
const typedPaths = [];
|
|
849
|
+
for (const path of touched) {
|
|
850
|
+
const before = input.visible.documents[path];
|
|
851
|
+
const after = resulting.documents[path];
|
|
852
|
+
if (typed(before) || typed(after))
|
|
853
|
+
typedPaths.push(path);
|
|
854
|
+
yield ROW_WEIGHT + (before?.length ?? 0) + (after?.length ?? 0);
|
|
855
|
+
}
|
|
407
856
|
const needsModel = typedPaths.length > 0;
|
|
408
857
|
const withoutModel = typedPaths.find((path) => governing(input.composition, path).status === 'unavailable');
|
|
409
858
|
if (withoutModel !== undefined)
|
|
410
859
|
return refuse('model-unavailable', withoutModel);
|
|
411
|
-
const
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
860
|
+
const verdict = clean !== null &&
|
|
861
|
+
(yield* changedCleanly({ text: resulting.documents, binaries: resulting.binaries }, clean, input.composition))
|
|
862
|
+
? { verdict: 'accept' }
|
|
863
|
+
: yield* validateCollection(needsModel
|
|
864
|
+
? resulting.documents
|
|
865
|
+
: Object.fromEntries(touched.flatMap((path) => resulting.documents[path] === undefined
|
|
866
|
+
? []
|
|
867
|
+
: [[path, resulting.documents[path]]])), Object.keys(resulting.binaries), input.composition);
|
|
415
868
|
if (verdict.verdict === 'refuse') {
|
|
416
869
|
return {
|
|
417
870
|
kind: 'refused',
|
|
418
871
|
refusal: {
|
|
419
|
-
|
|
872
|
+
code: verdict.rule,
|
|
873
|
+
retryable: false,
|
|
420
874
|
file: verdict.file,
|
|
421
875
|
...(verdict.location === undefined ? {} : { location: verdict.location }),
|
|
422
876
|
...(verdict.referrers === undefined ? {} : { referrers: verdict.referrers }),
|
|
423
877
|
},
|
|
424
878
|
};
|
|
425
879
|
}
|
|
426
|
-
const limited =
|
|
880
|
+
const limited = yield* exceeding(input.providerLimits, changes, predicted.changes, resulting);
|
|
427
881
|
if (limited !== null)
|
|
428
882
|
return limited;
|
|
429
883
|
const validated = typedPaths
|
|
@@ -449,3 +903,6 @@ export function buildBatch(input) {
|
|
|
449
903
|
}),
|
|
450
904
|
};
|
|
451
905
|
}
|
|
906
|
+
export function buildBatch(input) {
|
|
907
|
+
return stepped(batching(input));
|
|
908
|
+
}
|