@jini-ai/vibecoding 0.7.2 → 0.7.3-next.2
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/CHANGELOG.md +2 -2
- package/dist/core/history.js +111 -103
- package/dist/core/session.js +175 -143
- package/dist/html/node/parse5-region-parser.d.ts +1 -1
- package/dist/html/node/parse5-region-parser.js +1 -1
- package/dist/html/regions.js +67 -64
- package/dist/node/project.js +191 -166
- package/dist/node/store.d.ts +11 -11
- package/dist/node/store.js +290 -262
- package/dist/react/components/VibecodingWorkbench.d.ts +4 -1
- package/dist/react/components/VibecodingWorkbench.js +2 -2
- package/dist/tools.js +75 -60
- package/package.json +5 -5
package/dist/html/regions.js
CHANGED
|
@@ -102,6 +102,70 @@ function findOne(regions, id) {
|
|
|
102
102
|
// `matches[0]` is present: length is exactly 1 on this branch.
|
|
103
103
|
return { ok: true, region: matches[0] };
|
|
104
104
|
}
|
|
105
|
+
async function listRegionParts({ store, parser }) {
|
|
106
|
+
const regions = parser.findRegions({ html: await store.read() });
|
|
107
|
+
const counts = handleCounts(regions);
|
|
108
|
+
const refs = [];
|
|
109
|
+
for (const region of regions) {
|
|
110
|
+
// A malformed or duplicated handle is not publishable: publishing it would advertise an id
|
|
111
|
+
// that `readPart`/`replacePart` must then refuse, or worse, resolve ambiguously.
|
|
112
|
+
if (!isValidRegionHandle({ handle: region.handle }) || counts.get(region.handle) !== 1)
|
|
113
|
+
continue;
|
|
114
|
+
refs.push({
|
|
115
|
+
id: region.handle,
|
|
116
|
+
...(region.role === undefined ? {} : { kind: region.role }),
|
|
117
|
+
...(region.label === undefined ? {} : { label: region.label }),
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
return refs;
|
|
121
|
+
}
|
|
122
|
+
async function validateRegionCandidate({ store, parser }, candidate, maxPartLength) {
|
|
123
|
+
if (candidate.content.length > maxPartLength) {
|
|
124
|
+
return {
|
|
125
|
+
ok: false,
|
|
126
|
+
reason: `that content is ${candidate.content.length} characters, over the ${maxPartLength}-character limit for one region — send a shorter version`,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
const html = await store.read();
|
|
130
|
+
const before = parser.findRegions({ html });
|
|
131
|
+
const found = findOne(before, candidate.id);
|
|
132
|
+
if (!found.ok)
|
|
133
|
+
return { ok: false, reason: found.reason };
|
|
134
|
+
// Judge the WHOLE prospective document, never the fragment alone: a fragment that is
|
|
135
|
+
// well-formed by itself can still break the document it lands in.
|
|
136
|
+
const prospective = replaceRegionInner({ html, region: found.region, fragment: candidate.content });
|
|
137
|
+
const structural = parser.checkWellFormed?.({ html: prospective });
|
|
138
|
+
if (structural && !structural.ok)
|
|
139
|
+
return { ok: false, reason: structural.reason };
|
|
140
|
+
const afterHandles = handlesOf(parser.findRegions({ html: prospective }));
|
|
141
|
+
const beforeHandles = handlesOf(before);
|
|
142
|
+
// The allowlist check. Compared as multisets so an invented, deleted OR duplicated handle is
|
|
143
|
+
// all caught by one comparison — see this module's doc for why this is the security property.
|
|
144
|
+
const beforeKey = describeHandles(beforeHandles);
|
|
145
|
+
const afterKey = describeHandles(afterHandles);
|
|
146
|
+
if (beforeKey !== afterKey) {
|
|
147
|
+
const invented = afterHandles.filter((handle) => !beforeHandles.includes(handle));
|
|
148
|
+
const removed = beforeHandles.filter((handle) => !afterHandles.includes(handle));
|
|
149
|
+
const detail = handleChangeDetail({ invented, removed });
|
|
150
|
+
return {
|
|
151
|
+
ok: false,
|
|
152
|
+
reason: `${detail}. Edit only the content inside the region you were asked to change, and do not add, remove or duplicate ${AGENT_ELEMENT_ATTRIBUTE} attributes.`,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
return { ok: true };
|
|
156
|
+
}
|
|
157
|
+
async function snapshotRegions({ store, parser }) {
|
|
158
|
+
const html = await store.read();
|
|
159
|
+
const regions = parser.findRegions({ html });
|
|
160
|
+
const parts = {};
|
|
161
|
+
const counts = handleCounts(regions);
|
|
162
|
+
for (const region of regions) {
|
|
163
|
+
if (!isValidRegionHandle({ handle: region.handle }) || counts.get(region.handle) !== 1)
|
|
164
|
+
continue;
|
|
165
|
+
parts[region.handle] = html.slice(region.innerStart, region.innerEnd);
|
|
166
|
+
}
|
|
167
|
+
return { id: `html-${regions.length}-${html.length}`, parts };
|
|
168
|
+
}
|
|
105
169
|
/**
|
|
106
170
|
* Build an `EditTarget` over the tagged regions of one HTML document.
|
|
107
171
|
*
|
|
@@ -118,23 +182,7 @@ export function createHtmlRegionTarget(deps, options = {}) {
|
|
|
118
182
|
return replaceRegionInner({ html, region, fragment: content });
|
|
119
183
|
}
|
|
120
184
|
return {
|
|
121
|
-
|
|
122
|
-
const regions = parser.findRegions({ html: await store.read() });
|
|
123
|
-
const counts = handleCounts(regions);
|
|
124
|
-
const refs = [];
|
|
125
|
-
for (const region of regions) {
|
|
126
|
-
// A malformed or duplicated handle is not publishable: publishing it would advertise an id
|
|
127
|
-
// that `readPart`/`replacePart` must then refuse, or worse, resolve ambiguously.
|
|
128
|
-
if (!isValidRegionHandle({ handle: region.handle }) || counts.get(region.handle) !== 1)
|
|
129
|
-
continue;
|
|
130
|
-
refs.push({
|
|
131
|
-
id: region.handle,
|
|
132
|
-
...(region.role === undefined ? {} : { kind: region.role }),
|
|
133
|
-
...(region.label === undefined ? {} : { label: region.label }),
|
|
134
|
-
});
|
|
135
|
-
}
|
|
136
|
-
return refs;
|
|
137
|
-
},
|
|
185
|
+
listParts: () => listRegionParts(deps),
|
|
138
186
|
async readPart({ id }) {
|
|
139
187
|
const html = await store.read();
|
|
140
188
|
const found = findOne(parser.findRegions({ html }), id);
|
|
@@ -155,18 +203,7 @@ export function createHtmlRegionTarget(deps, options = {}) {
|
|
|
155
203
|
throw new Error(found.reason);
|
|
156
204
|
await store.write({ html: spliced(html, found.region, content) });
|
|
157
205
|
},
|
|
158
|
-
|
|
159
|
-
const html = await store.read();
|
|
160
|
-
const regions = parser.findRegions({ html });
|
|
161
|
-
const parts = {};
|
|
162
|
-
const counts = handleCounts(regions);
|
|
163
|
-
for (const region of regions) {
|
|
164
|
-
if (!isValidRegionHandle({ handle: region.handle }) || counts.get(region.handle) !== 1)
|
|
165
|
-
continue;
|
|
166
|
-
parts[region.handle] = html.slice(region.innerStart, region.innerEnd);
|
|
167
|
-
}
|
|
168
|
-
return { id: `html-${regions.length}-${html.length}`, parts };
|
|
169
|
-
},
|
|
206
|
+
snapshot: () => snapshotRegions(deps),
|
|
170
207
|
/**
|
|
171
208
|
* Restores each region's content in one pass.
|
|
172
209
|
*
|
|
@@ -183,41 +220,7 @@ export function createHtmlRegionTarget(deps, options = {}) {
|
|
|
183
220
|
await store.write({ html: spliced(html, found.region, content) });
|
|
184
221
|
}
|
|
185
222
|
},
|
|
186
|
-
|
|
187
|
-
if (candidate.content.length > maxPartLength) {
|
|
188
|
-
return {
|
|
189
|
-
ok: false,
|
|
190
|
-
reason: `that content is ${candidate.content.length} characters, over the ${maxPartLength}-character limit for one region — send a shorter version`,
|
|
191
|
-
};
|
|
192
|
-
}
|
|
193
|
-
const html = await store.read();
|
|
194
|
-
const before = parser.findRegions({ html });
|
|
195
|
-
const found = findOne(before, candidate.id);
|
|
196
|
-
if (!found.ok)
|
|
197
|
-
return { ok: false, reason: found.reason };
|
|
198
|
-
// Judge the WHOLE prospective document, never the fragment alone: a fragment that is
|
|
199
|
-
// well-formed by itself can still break the document it lands in.
|
|
200
|
-
const prospective = spliced(html, found.region, candidate.content);
|
|
201
|
-
const structural = parser.checkWellFormed?.({ html: prospective });
|
|
202
|
-
if (structural && !structural.ok)
|
|
203
|
-
return { ok: false, reason: structural.reason };
|
|
204
|
-
const afterHandles = handlesOf(parser.findRegions({ html: prospective }));
|
|
205
|
-
const beforeHandles = handlesOf(before);
|
|
206
|
-
// The allowlist check. Compared as multisets so an invented, deleted OR duplicated handle is
|
|
207
|
-
// all caught by one comparison — see this module's doc for why this is the security property.
|
|
208
|
-
const beforeKey = describeHandles(beforeHandles);
|
|
209
|
-
const afterKey = describeHandles(afterHandles);
|
|
210
|
-
if (beforeKey !== afterKey) {
|
|
211
|
-
const invented = afterHandles.filter((handle) => !beforeHandles.includes(handle));
|
|
212
|
-
const removed = beforeHandles.filter((handle) => !afterHandles.includes(handle));
|
|
213
|
-
const detail = handleChangeDetail({ invented, removed });
|
|
214
|
-
return {
|
|
215
|
-
ok: false,
|
|
216
|
-
reason: `${detail}. Edit only the content inside the region you were asked to change, and do not add, remove or duplicate ${AGENT_ELEMENT_ATTRIBUTE} attributes.`,
|
|
217
|
-
};
|
|
218
|
-
}
|
|
219
|
-
return { ok: true };
|
|
220
|
-
},
|
|
223
|
+
validate: (candidate) => validateRegionCandidate(deps, candidate, maxPartLength),
|
|
221
224
|
};
|
|
222
225
|
}
|
|
223
226
|
//# sourceMappingURL=regions.js.map
|
package/dist/node/project.js
CHANGED
|
@@ -131,184 +131,204 @@ export async function openFileTreeProject(input, options = {}) {
|
|
|
131
131
|
* Validation releases the shared lock, then checks generation AND working-tree drift again.
|
|
132
132
|
*/
|
|
133
133
|
function assembleProject({ store, paths, limits, historyLimit, validator }) {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
const
|
|
134
|
+
const state = { current: null, closed: false, selectingCapture: undefined, queue: Promise.resolve() };
|
|
135
|
+
const pointer = createRevisionPointer(state, store);
|
|
136
|
+
const rawHistory = createEditHistory({ target: pointer }, { limit: historyLimit });
|
|
137
|
+
const context = { store, paths, limits, historyLimit, validator, state, rawHistory };
|
|
138
|
+
return createProjectAdapters(context);
|
|
139
|
+
}
|
|
140
|
+
function createRevisionPointer(state, store) {
|
|
141
|
+
const context = { state };
|
|
142
|
+
return {
|
|
138
143
|
listParts: async () => [{ id: POINTER }],
|
|
139
|
-
readPart: async () => selected().revision,
|
|
144
|
+
readPart: async () => selected(context).revision,
|
|
140
145
|
replacePart: async ({ id, content }) => {
|
|
141
146
|
if (id !== POINTER)
|
|
142
147
|
fail('INVALID_CHANGE', 'private revision pointer only');
|
|
143
|
-
if (!selectingCapture)
|
|
144
|
-
await store.preflight({ current, revisionId: content });
|
|
145
|
-
current = await store.select({ current: selected(), revisionId: content, captured: selectingCapture });
|
|
148
|
+
if (!context.state.selectingCapture)
|
|
149
|
+
await store.preflight({ current: context.state.current, revisionId: content });
|
|
150
|
+
context.state.current = await store.select({ current: selected(context), revisionId: content, captured: context.state.selectingCapture });
|
|
146
151
|
},
|
|
147
152
|
validate: async () => ({ ok: true }),
|
|
148
|
-
snapshot: async () => ({ id: selected().revision, parts: { [POINTER]: selected().revision } }),
|
|
153
|
+
snapshot: async () => ({ id: selected(context).revision, parts: { [POINTER]: selected(context).revision } }),
|
|
149
154
|
restore: async () => { fail('INVALID_CHANGE', 'use project.restore'); },
|
|
150
155
|
};
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
+
}
|
|
157
|
+
function selected(context) {
|
|
158
|
+
if (!context.state.current)
|
|
159
|
+
fail('CORRUPT', 'project has no current revision');
|
|
160
|
+
return context.state.current;
|
|
161
|
+
}
|
|
162
|
+
function serial(context, work) {
|
|
163
|
+
const next = context.state.queue.then(async () => {
|
|
164
|
+
if (context.state.closed)
|
|
165
|
+
fail('CLOSED', 'project is closed');
|
|
166
|
+
return work();
|
|
167
|
+
});
|
|
168
|
+
context.state.queue = next.catch(() => { });
|
|
169
|
+
return next;
|
|
170
|
+
}
|
|
171
|
+
/** Never replay a local stack over a different writer's generation, even after A→B→A. */
|
|
172
|
+
async function synchronize(context) {
|
|
173
|
+
const { store, rawHistory } = context;
|
|
174
|
+
const disk = await store.recover();
|
|
175
|
+
if (context.state.current && (!disk || !sameRevision(revisionToken({ current: context.state.current }), revisionToken({ current: disk })))) {
|
|
176
|
+
context.state.current = disk;
|
|
177
|
+
rawHistory.clear();
|
|
178
|
+
fail('CONFLICT', 'project generation changed; local history cleared');
|
|
156
179
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
});
|
|
163
|
-
queue = next.catch(() => { });
|
|
164
|
-
return next;
|
|
180
|
+
context.state.current = disk;
|
|
181
|
+
}
|
|
182
|
+
async function locked(context, work) {
|
|
183
|
+
const { store } = context;
|
|
184
|
+
try {
|
|
185
|
+
return await store.locked(async () => { await synchronize(context); return work(); });
|
|
165
186
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
if (
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
}
|
|
174
|
-
current = disk;
|
|
187
|
+
catch (cause) {
|
|
188
|
+
// Only synchronize() clears history: another writer's generation makes the stacks unsound.
|
|
189
|
+
// A stale caller token or a file written mid-scan leaves this handle's stacks coherent.
|
|
190
|
+
if (cause instanceof ProjectError)
|
|
191
|
+
throw cause;
|
|
192
|
+
if (cause instanceof FsFilePathError)
|
|
193
|
+
throw new ProjectError({ code: 'INVALID_PATH', message: cause.message, cause });
|
|
194
|
+
throw new ProjectError({ code: 'STORAGE', message: 'project storage operation failed', cause });
|
|
175
195
|
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
if (cause instanceof FsFilePathError)
|
|
186
|
-
throw new ProjectError({ code: 'INVALID_PATH', message: cause.message, cause });
|
|
187
|
-
throw new ProjectError({ code: 'STORAGE', message: 'project storage operation failed', cause });
|
|
188
|
-
}
|
|
196
|
+
}
|
|
197
|
+
/** The pointer write is the only and last write inside history's recording callback. */
|
|
198
|
+
async function record(context, { revisionId, captured, label }) {
|
|
199
|
+
const { store, rawHistory } = context;
|
|
200
|
+
if (!captured)
|
|
201
|
+
await store.preflight({ current: context.state.current, revisionId });
|
|
202
|
+
context.state.selectingCapture = captured;
|
|
203
|
+
try {
|
|
204
|
+
await rawHistory.transaction({ work: recording => recording.replacePart({ id: POINTER, content: revisionId }) }, label === undefined ? {} : { label });
|
|
189
205
|
}
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
if (!captured)
|
|
193
|
-
await store.preflight({ current, revisionId });
|
|
194
|
-
selectingCapture = captured;
|
|
195
|
-
try {
|
|
196
|
-
await rawHistory.transaction({ work: recording => recording.replacePart({ id: POINTER, content: revisionId }) }, label === undefined ? {} : { label });
|
|
197
|
-
}
|
|
198
|
-
finally {
|
|
199
|
-
selectingCapture = undefined;
|
|
200
|
-
}
|
|
206
|
+
finally {
|
|
207
|
+
context.state.selectingCapture = undefined;
|
|
201
208
|
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
+
}
|
|
210
|
+
async function collect(context) {
|
|
211
|
+
const { store, rawHistory } = context;
|
|
212
|
+
const revisions = new Set([selected(context).revision]);
|
|
213
|
+
for (const entry of rawHistory.retainedEntries()) {
|
|
214
|
+
for (const change of entry.changes) {
|
|
215
|
+
revisions.add(change.before);
|
|
216
|
+
revisions.add(change.after);
|
|
209
217
|
}
|
|
210
|
-
await store.collect({ revisions });
|
|
211
|
-
}
|
|
212
|
-
/** Drift becomes a normal revision without overwriting what npm/editor/user just wrote. */
|
|
213
|
-
async function captureLocked() {
|
|
214
|
-
const { files, observed } = await store.scan({ current });
|
|
215
|
-
const revisionId = await store.prepare({ files });
|
|
216
|
-
const previous = current?.revision;
|
|
217
|
-
if (!current)
|
|
218
|
-
current = await store.select({ current: null, revisionId, captured: observed });
|
|
219
|
-
else if (current.revision !== revisionId)
|
|
220
|
-
await record({ revisionId, captured: observed, label: 'Capture external changes' });
|
|
221
|
-
else
|
|
222
|
-
current = await store.observe({ current, observed });
|
|
223
|
-
if (previous !== revisionId)
|
|
224
|
-
await collect();
|
|
225
|
-
return files;
|
|
226
218
|
}
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
219
|
+
await store.collect({ revisions });
|
|
220
|
+
}
|
|
221
|
+
/** Drift becomes a normal revision without overwriting what npm/editor/user just wrote. */
|
|
222
|
+
async function captureLocked(context) {
|
|
223
|
+
const { store } = context;
|
|
224
|
+
const { files, observed } = await store.scan({ current: context.state.current });
|
|
225
|
+
const revisionId = await store.prepare({ files });
|
|
226
|
+
const previous = context.state.current?.revision;
|
|
227
|
+
if (!context.state.current)
|
|
228
|
+
context.state.current = await store.select({ current: null, revisionId, captured: observed });
|
|
229
|
+
else if (context.state.current.revision !== revisionId)
|
|
230
|
+
await record(context, { revisionId, captured: observed, label: 'Capture external changes' });
|
|
231
|
+
else
|
|
232
|
+
context.state.current = await store.observe({ current: context.state.current, observed });
|
|
233
|
+
if (previous !== revisionId)
|
|
234
|
+
await collect(context);
|
|
235
|
+
return files;
|
|
236
|
+
}
|
|
237
|
+
/** The caller's view is stale (e.g. drift captured as its own entry); history stays intact. */
|
|
238
|
+
function checkExpected(context, expected) {
|
|
239
|
+
if (sameRevision(expected, revisionToken({ current: selected(context) })))
|
|
240
|
+
return;
|
|
241
|
+
fail('CONFLICT', 'expected revision is stale; capture/refresh and retry');
|
|
242
|
+
}
|
|
243
|
+
async function applyTurn(context, input, options = {}) {
|
|
244
|
+
const { paths, limits, validator } = context;
|
|
245
|
+
return serial(context, async () => {
|
|
246
|
+
const files = await locked(context, async () => {
|
|
247
|
+
const base = await captureLocked(context);
|
|
248
|
+
checkExpected(context, input.expectedRevision);
|
|
249
|
+
return changeFiles({ files: base, changes: input.changes, paths, limits });
|
|
251
250
|
});
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
return
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
251
|
+
const diagnostics = await validateCandidate({ validator, candidate: candidateView({ files }), signal: options.signal, timeoutMs: limits.validationTimeoutMs });
|
|
252
|
+
return locked(context, async () => {
|
|
253
|
+
// Validation can run arbitrary host code; do not silently erase edits made while it ran.
|
|
254
|
+
await captureLocked(context);
|
|
255
|
+
checkExpected(context, input.expectedRevision);
|
|
256
|
+
if (options.signal?.aborted)
|
|
257
|
+
fail('CANCELLED', 'turn cancelled');
|
|
258
|
+
if (diagnostics.length)
|
|
259
|
+
return { status: 'rejected', revision: revisionToken({ current: selected(context) }), diagnostics };
|
|
260
|
+
return commitFiles(context, { files, label: options.label });
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
async function commitFiles(context, { files, label }) {
|
|
265
|
+
const { store } = context;
|
|
266
|
+
const revisionId = await store.prepare({ files });
|
|
267
|
+
if (revisionId === selected(context).revision)
|
|
268
|
+
return { status: 'unchanged', revision: revisionToken({ current: selected(context) }), diagnostics: [] };
|
|
269
|
+
await record(context, { revisionId, ...(label === undefined ? {} : { label }) });
|
|
270
|
+
await collect(context);
|
|
271
|
+
return { status: 'committed', revision: revisionToken({ current: selected(context) }), diagnostics: [] };
|
|
272
|
+
}
|
|
273
|
+
async function replay(context, side) {
|
|
274
|
+
const { rawHistory } = context;
|
|
275
|
+
return serial(context, () => locked(context, async () => {
|
|
276
|
+
await captureLocked(context);
|
|
277
|
+
const entry = await rawHistory[side]();
|
|
278
|
+
await collect(context);
|
|
279
|
+
return entry;
|
|
280
|
+
}));
|
|
281
|
+
}
|
|
282
|
+
async function restoreRevision(context, { revisionId, label }) {
|
|
283
|
+
const { store } = context;
|
|
284
|
+
return serial(context, () => locked(context, async () => {
|
|
285
|
+
await captureLocked(context);
|
|
286
|
+
await retainedManifest({ store, revisionId });
|
|
287
|
+
if (selected(context).revision !== revisionId)
|
|
288
|
+
await record(context, { revisionId, ...(label === undefined ? {} : { label }) });
|
|
289
|
+
await collect(context);
|
|
290
|
+
return revisionToken({ current: selected(context) });
|
|
291
|
+
}));
|
|
292
|
+
}
|
|
293
|
+
async function textSnapshot(context) {
|
|
294
|
+
return serial(context, () => locked(context, async () => {
|
|
295
|
+
const files = await captureLocked(context);
|
|
296
|
+
const parts = Object.create(null);
|
|
297
|
+
for (const [path, bytes] of files)
|
|
298
|
+
parts[path] = decodeText({ bytes });
|
|
299
|
+
return { id: selected(context).revision, parts };
|
|
300
|
+
}));
|
|
301
|
+
}
|
|
302
|
+
async function restoreText(context, { snapshot }) {
|
|
303
|
+
const { rawHistory } = context;
|
|
304
|
+
// Snapshot IDs name retained manifests. Replaying them preserves binary files and deletions.
|
|
305
|
+
const before = rawHistory.entries().at(-1);
|
|
306
|
+
await restoreRevision(context, { revisionId: snapshot.id, label: `restore ${snapshot.id}` });
|
|
307
|
+
const after = rawHistory.entries().at(-1);
|
|
308
|
+
return before === after ? null : after ?? null;
|
|
309
|
+
}
|
|
310
|
+
function createProjectHistory(context) {
|
|
311
|
+
const { rawHistory } = context;
|
|
312
|
+
return {
|
|
296
313
|
transaction: async () => fail('INVALID_CHANGE', 'use project.applyTurn for atomic transactions'),
|
|
297
314
|
canUndo: rawHistory.canUndo, canRedo: rawHistory.canRedo,
|
|
298
|
-
undo: () => replay('undo'), redo: () => replay('redo'), restore: restoreText,
|
|
315
|
+
undo: () => replay(context, 'undo'), redo: () => replay(context, 'redo'), restore: input => restoreText(context, input),
|
|
299
316
|
entries: rawHistory.entries, retainedEntries: rawHistory.retainedEntries, clear: rawHistory.clear,
|
|
300
317
|
};
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
318
|
+
}
|
|
319
|
+
async function listPartsLocked(context) { return [...(await captureLocked(context)).keys()].sort().map(id => ({ id })); }
|
|
320
|
+
async function readPartLocked(context, id) {
|
|
321
|
+
const { paths, store } = context;
|
|
322
|
+
paths.check({ relativePath: id });
|
|
323
|
+
const manifest = await store.manifest({ revisionId: selected(context).revision });
|
|
324
|
+
if (!manifest.files.some(file => file.path === id))
|
|
325
|
+
fail('INVALID_CHANGE', `file not listed: ${id}`);
|
|
326
|
+
return decodeText({ bytes: await store.readWorkingFile({ relativePath: id }) });
|
|
327
|
+
}
|
|
328
|
+
function createProjectTarget(context, applyEdits) {
|
|
329
|
+
return {
|
|
330
|
+
listParts: () => serial(context, () => locked(context, () => listPartsLocked(context))),
|
|
331
|
+
readPart: ({ id }) => serial(context, () => locked(context, () => readPartLocked(context, id))),
|
|
312
332
|
replacePart: async (edit) => {
|
|
313
333
|
const outcomes = await applyEdits({ edits: [edit] });
|
|
314
334
|
const outcome = outcomes[0];
|
|
@@ -318,13 +338,15 @@ function assembleProject({ store, paths, limits, historyLimit, validator }) {
|
|
|
318
338
|
fail('VALIDATION', outcome.reason);
|
|
319
339
|
},
|
|
320
340
|
validate: async () => ({ ok: true }), // replacePart owns whole-candidate validation, including direct calls.
|
|
321
|
-
snapshot: textSnapshot, restore: async (input) => { await restoreText(input); },
|
|
341
|
+
snapshot: () => textSnapshot(context), restore: async (input) => { await restoreText(context, input); },
|
|
322
342
|
};
|
|
343
|
+
}
|
|
344
|
+
function createProjectAdapters(context) {
|
|
323
345
|
async function applyEdits({ edits }, options = {}) {
|
|
324
346
|
const expectedRevision = await project.capture();
|
|
325
347
|
const changes = edits.map(edit => ({ op: 'replace', ...edit }));
|
|
326
348
|
try {
|
|
327
|
-
const result = await applyTurn({ expectedRevision, changes }, options);
|
|
349
|
+
const result = await applyTurn(context, { expectedRevision, changes }, options);
|
|
328
350
|
if (result.status !== 'rejected')
|
|
329
351
|
return edits.map(({ id }) => ({ status: 'applied', id }));
|
|
330
352
|
const reason = result.diagnostics.map(diagnostic => diagnostic.message).join('\n');
|
|
@@ -335,13 +357,16 @@ function assembleProject({ store, paths, limits, historyLimit, validator }) {
|
|
|
335
357
|
return edits.map(({ id }) => ({ status: 'failed', id, error: failure }));
|
|
336
358
|
}
|
|
337
359
|
}
|
|
360
|
+
const history = createProjectHistory(context);
|
|
361
|
+
const target = createProjectTarget(context, applyEdits);
|
|
362
|
+
const { rawHistory } = context;
|
|
338
363
|
const project = {
|
|
339
|
-
target, history, applyTurn, applyEdits,
|
|
340
|
-
getRevision: () => serial(() => locked(async () => revisionToken({ current: selected() }))),
|
|
341
|
-
capture: () => serial(() => locked(async () => { await captureLocked(); await collect(); return revisionToken({ current: selected() }); })),
|
|
342
|
-
snapshot: () => serial(() => locked(async () => { await captureLocked(); return { revisionId: selected().revision }; })),
|
|
343
|
-
restore: ({ snapshot }, options = {}) => restoreRevision({ revisionId: snapshot.revisionId, ...options }),
|
|
344
|
-
close: () => serial(() => locked(async () => { rawHistory.clear(); await collect(); closed = true; })),
|
|
364
|
+
target, history, applyTurn: (input, options = {}) => applyTurn(context, input, options), applyEdits,
|
|
365
|
+
getRevision: () => serial(context, () => locked(context, async () => revisionToken({ current: selected(context) }))),
|
|
366
|
+
capture: () => serial(context, () => locked(context, async () => { await captureLocked(context); await collect(context); return revisionToken({ current: selected(context) }); })),
|
|
367
|
+
snapshot: () => serial(context, () => locked(context, async () => { await captureLocked(context); return { revisionId: selected(context).revision }; })),
|
|
368
|
+
restore: ({ snapshot }, options = {}) => restoreRevision(context, { revisionId: snapshot.revisionId, ...options }),
|
|
369
|
+
close: () => serial(context, () => locked(context, async () => { rawHistory.clear(); await collect(context); context.state.closed = true; })),
|
|
345
370
|
};
|
|
346
371
|
return project;
|
|
347
372
|
}
|
package/dist/node/store.d.ts
CHANGED
|
@@ -59,40 +59,40 @@ export declare function prepareDirectories({ root, stateDir }: {
|
|
|
59
59
|
*/
|
|
60
60
|
export declare function createProjectStore({ root, stateDir, paths, limits, filesystem }: StoreInput, options?: StoreOptions): {
|
|
61
61
|
readCurrent: () => Promise<Current | null>;
|
|
62
|
-
manifest: (
|
|
62
|
+
manifest: (input: {
|
|
63
63
|
revisionId: string;
|
|
64
64
|
}) => Promise<Manifest>;
|
|
65
|
-
object: (
|
|
65
|
+
object: (input: {
|
|
66
66
|
file: ManifestFile;
|
|
67
|
-
}) => Promise<Uint8Array
|
|
68
|
-
prepare: (
|
|
67
|
+
}) => Promise<Uint8Array<ArrayBufferLike>>;
|
|
68
|
+
prepare: (input: {
|
|
69
69
|
files: ReadonlyMap<string, Uint8Array>;
|
|
70
70
|
}) => Promise<string>;
|
|
71
|
-
scan: (
|
|
71
|
+
scan: (input: {
|
|
72
72
|
current: Current | null;
|
|
73
73
|
}) => Promise<{
|
|
74
74
|
files: Map<string, Uint8Array>;
|
|
75
75
|
observed: Record<string, Fingerprint>;
|
|
76
76
|
}>;
|
|
77
|
-
select: (
|
|
77
|
+
select: (input: {
|
|
78
78
|
current: Current | null;
|
|
79
79
|
revisionId: string;
|
|
80
80
|
captured?: Readonly<Record<string, Fingerprint>> | undefined;
|
|
81
81
|
}) => Promise<Current>;
|
|
82
|
-
collect: (
|
|
82
|
+
collect: (input: {
|
|
83
83
|
revisions: ReadonlySet<string>;
|
|
84
84
|
}) => Promise<void>;
|
|
85
|
-
preflight: (
|
|
85
|
+
preflight: (input: {
|
|
86
86
|
current: Current | null;
|
|
87
87
|
revisionId: string;
|
|
88
88
|
}) => Promise<void>;
|
|
89
|
-
observe: (
|
|
89
|
+
observe: (input: {
|
|
90
90
|
current: Current;
|
|
91
91
|
observed: Current["observed"];
|
|
92
92
|
}) => Promise<Current>;
|
|
93
|
-
readWorkingFile: (
|
|
93
|
+
readWorkingFile: (input: {
|
|
94
94
|
relativePath: string;
|
|
95
|
-
}) => Promise<Uint8Array
|
|
95
|
+
}) => Promise<Uint8Array<ArrayBufferLike>>;
|
|
96
96
|
locked: <T>(run: () => Promise<T>) => Promise<T>;
|
|
97
97
|
recover: () => Promise<Current | null>;
|
|
98
98
|
};
|