@vrtmrz/livesync-commonlib 0.4.1 → 0.4.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/README.md +14 -2
- package/dist/interfaces/FileHandler.d.ts +59 -0
- package/dist/interfaces/FileReflectionProvenance.d.ts +15 -1
- package/dist/interfaces/FileReflectionProvenance.js +11 -1
- package/dist/serviceFeatures/offlineScanner/fileStatus.d.ts +61 -0
- package/dist/serviceFeatures/offlineScanner/fileStatus.js +85 -0
- package/dist/serviceFeatures/offlineScanner/files.d.ts +47 -0
- package/dist/serviceFeatures/offlineScanner/files.js +218 -0
- package/dist/serviceFeatures/offlineScanner/metadata.d.ts +56 -0
- package/dist/serviceFeatures/offlineScanner/metadata.js +320 -0
- package/dist/serviceFeatures/offlineScanner/paths.d.ts +20 -0
- package/dist/serviceFeatures/offlineScanner/paths.js +16 -0
- package/dist/serviceFeatures/offlineScanner/scan.d.ts +35 -0
- package/dist/serviceFeatures/offlineScanner/scan.js +358 -0
- package/dist/serviceFeatures/offlineScanner/types.d.ts +134 -0
- package/dist/serviceFeatures/offlineScanner/types.js +66 -0
- package/dist/serviceFeatures/offlineScanner.d.ts +25 -251
- package/dist/serviceFeatures/offlineScanner.js +36 -860
- package/dist/serviceModules/ServiceFileHandlerBase/deletionRecovery.d.ts +13 -0
- package/dist/serviceModules/ServiceFileHandlerBase/deletionRecovery.js +194 -0
- package/dist/serviceModules/ServiceFileHandlerBase.d.ts +22 -1
- package/dist/serviceModules/ServiceFileHandlerBase.js +29 -0
- package/dist/services/base/KeyValueDBService.d.ts +9 -0
- package/dist/services/base/KeyValueDBService.js +11 -1
- package/docs/development.md +4 -0
- package/docs/offline-scanner.md +21 -0
- package/docs/storage-events-and-reflection.md +16 -10
- package/package.json +1 -1
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { MetaEntry } from "../../common/types.js";
|
|
2
|
+
import type { DeletionRecoveryResult } from "../../interfaces/FileHandler.js";
|
|
3
|
+
import type { FileHandlerContext } from "./context.js";
|
|
4
|
+
/**
|
|
5
|
+
* Verify and recover one retained logical deletion without creating revision history.
|
|
6
|
+
* @param ctx - Captured services, state, and dynamic host boundaries.
|
|
7
|
+
* @param expected - The exact logical deletion selected by the ordinary scan.
|
|
8
|
+
* @param cleanupOnly - Require current absence and clear bookkeeping without reading or deleting a file.
|
|
9
|
+
* @returns Separate observed file, provenance, and completion outcomes, including verification failures.
|
|
10
|
+
* @remarks A shared three-operation permit precedes the document lock and all I/O. The lock
|
|
11
|
+
* excludes other handlers, while final observations conservatively detect external changes.
|
|
12
|
+
*/
|
|
13
|
+
export declare function recoverDatabaseDeletion(ctx: FileHandlerContext, expected: MetaEntry, cleanupOnly?: boolean): Promise<DeletionRecoveryResult>;
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { Semaphore, withSemaphore } from "octagonal-wheels/concurrency/semaphore";
|
|
2
|
+
import { TARGET_IS_NEW } from "../../common/models/shared.const.symbols.js";
|
|
3
|
+
import { isDocContentSame, readContent } from "../../common/utils.js";
|
|
4
|
+
import { isFilePath } from "../../string_and_binary/path.js";
|
|
5
|
+
import { withFileEventPaths } from "./locking.js";
|
|
6
|
+
import {
|
|
7
|
+
contentEvidence,
|
|
8
|
+
getProvenancePath,
|
|
9
|
+
hasContentHash,
|
|
10
|
+
readProvenanceSnapshot,
|
|
11
|
+
sameProvenance
|
|
12
|
+
} from "./provenance.js";
|
|
13
|
+
import { isCurrentFileSelected, isCurrentPathSelected } from "./selection.js";
|
|
14
|
+
import { isFolderInfo } from "./storage.js";
|
|
15
|
+
const recoveryPermits = /* @__PURE__ */ new WeakMap();
|
|
16
|
+
function progressAttempt(attempt, progress) {
|
|
17
|
+
Object.assign(attempt, progress);
|
|
18
|
+
}
|
|
19
|
+
async function recoverDatabaseDeletion(ctx, expected, cleanupOnly = false) {
|
|
20
|
+
const attempt = {
|
|
21
|
+
ctx,
|
|
22
|
+
expected,
|
|
23
|
+
path: expected.path,
|
|
24
|
+
file: "unknown",
|
|
25
|
+
provenance: "unchanged",
|
|
26
|
+
phase: "admission"
|
|
27
|
+
};
|
|
28
|
+
if (!ctx.provenance?.listPaths) return outcome(attempt, "deferred", "capability-unavailable");
|
|
29
|
+
let semaphore = recoveryPermits.get(ctx.provenance);
|
|
30
|
+
if (!semaphore) {
|
|
31
|
+
semaphore = Semaphore(3);
|
|
32
|
+
recoveryPermits.set(ctx.provenance, semaphore);
|
|
33
|
+
}
|
|
34
|
+
try {
|
|
35
|
+
return await withSemaphore(semaphore, () => {
|
|
36
|
+
progressAttempt(attempt, { path: ctx.host.getPath(expected) });
|
|
37
|
+
return withFileEventPaths(ctx, [attempt.path], () => recoverUnderLock(attempt, cleanupOnly));
|
|
38
|
+
});
|
|
39
|
+
} catch (error) {
|
|
40
|
+
return { ...outcome(attempt, "failed", `${attempt.phase}-failed`), error };
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
async function recoverUnderLock(attempt, cleanupOnly) {
|
|
44
|
+
const { ctx, expected, path } = attempt;
|
|
45
|
+
progressAttempt(attempt, { phase: "eligibility" });
|
|
46
|
+
if (!validDeletion(expected) || !isFilePath(path) || await ctx.path.path2id(path) !== expected._id) {
|
|
47
|
+
return outcome(attempt, "deferred", "invalid-deletion");
|
|
48
|
+
}
|
|
49
|
+
if (!await isCurrentPathSelected(ctx, path)) return outcome(attempt, "deferred", "not-selected");
|
|
50
|
+
if (!await sameCurrentDeletion(attempt)) return outcome(attempt, "deferred", "database-changed");
|
|
51
|
+
const stub = await ctx.host.storage().getStub(path);
|
|
52
|
+
progressAttempt(attempt, {
|
|
53
|
+
file: stub === null ? "absent" : "present",
|
|
54
|
+
provenance: stub === null ? "pending" : "unchanged"
|
|
55
|
+
});
|
|
56
|
+
if (isFolderInfo(stub)) return outcome(attempt, "deferred", "storage-identity-changed");
|
|
57
|
+
if (stub !== null && (stub.isInternal || stub.path !== path)) {
|
|
58
|
+
return outcome(attempt, "deferred", "storage-identity-changed");
|
|
59
|
+
}
|
|
60
|
+
if (cleanupOnly && stub !== null) return outcome(attempt, "deferred", "file-present");
|
|
61
|
+
if (stub && (!await isCurrentFileSelected(ctx, stub) || ctx.path.compareFileFreshness(stub, expected) !== TARGET_IS_NEW)) {
|
|
62
|
+
return outcome(attempt, "deferred", "file-not-eligible");
|
|
63
|
+
}
|
|
64
|
+
progressAttempt(attempt, { phase: "provenance-read" });
|
|
65
|
+
const record = await readProvenanceSnapshot(ctx, path, true);
|
|
66
|
+
if (!stub) return await clearAbsentProvenance(attempt, record);
|
|
67
|
+
if (!record?.revision) return outcome(attempt, "deferred", "provenance-missing");
|
|
68
|
+
if (hasContentHash(record) && record.contentSize !== stub.stat.size) {
|
|
69
|
+
return outcome(attempt, "deferred", "content-size-mismatch");
|
|
70
|
+
}
|
|
71
|
+
return await verifyPresentFile(attempt, { ...stub, stat: { ...stub.stat } }, record);
|
|
72
|
+
}
|
|
73
|
+
async function verifyPresentFile(attempt, stub, record) {
|
|
74
|
+
const { ctx, path } = attempt;
|
|
75
|
+
let base;
|
|
76
|
+
if (!hasContentHash(record)) {
|
|
77
|
+
progressAttempt(attempt, { phase: "base-read" });
|
|
78
|
+
base = await ctx.host.db().fetchEntry(path, record.revision, true, true, false);
|
|
79
|
+
if (!base) return outcome(attempt, "failed", "base-unavailable");
|
|
80
|
+
if (base._rev !== record.revision || base.deleted || base._deleted || ctx.host.getPath(base) !== path) {
|
|
81
|
+
return outcome(attempt, "deferred", "base-not-usable");
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
progressAttempt(attempt, { phase: "body-read" });
|
|
85
|
+
const observed = await ctx.host.readFileFromStub(stub);
|
|
86
|
+
const file = { ...observed, stat: { ...observed.stat } };
|
|
87
|
+
if (!sameFileObservation(stub, file)) return outcome(attempt, "deferred", "storage-changed");
|
|
88
|
+
if (hasContentHash(record)) {
|
|
89
|
+
progressAttempt(attempt, { phase: "content-hash" });
|
|
90
|
+
const evidence = await contentEvidence(ctx, file.body);
|
|
91
|
+
if (!evidence.contentHash) return outcome(attempt, "failed", "content-hash-unavailable");
|
|
92
|
+
if (evidence.contentSize !== record.contentSize || evidence.contentHash !== record.contentHash) {
|
|
93
|
+
return outcome(attempt, "deferred", "content-hash-mismatch");
|
|
94
|
+
}
|
|
95
|
+
} else if (!base || !await isDocContentSame(readContent(base), file.body)) {
|
|
96
|
+
return outcome(attempt, "deferred", "base-content-mismatch");
|
|
97
|
+
}
|
|
98
|
+
base = void 0;
|
|
99
|
+
progressAttempt(attempt, { phase: "final-verification" });
|
|
100
|
+
const latest = await ctx.host.storage().getStub(path);
|
|
101
|
+
if (!latest || isFolderInfo(latest) || !sameFileObservation(latest, file)) {
|
|
102
|
+
progressAttempt(attempt, { file: latest === null ? "absent" : "present", provenance: "unchanged" });
|
|
103
|
+
return outcome(attempt, "deferred", "storage-changed");
|
|
104
|
+
}
|
|
105
|
+
const readFinal = await ctx.host.readFileFromStub(latest);
|
|
106
|
+
const final = { ...readFinal, stat: { ...readFinal.stat } };
|
|
107
|
+
if (!sameFileObservation(final, file) || !await isDocContentSame(final.body, file.body)) {
|
|
108
|
+
return outcome(attempt, "deferred", "storage-changed");
|
|
109
|
+
}
|
|
110
|
+
if (!sameProvenance(await readProvenanceSnapshot(ctx, path, true), record)) {
|
|
111
|
+
return outcome(attempt, "deferred", "provenance-changed");
|
|
112
|
+
}
|
|
113
|
+
if (!await sameCurrentDeletion(attempt)) return outcome(attempt, "deferred", "database-changed");
|
|
114
|
+
if (!await isCurrentFileSelected(ctx, final)) return outcome(attempt, "deferred", "not-selected");
|
|
115
|
+
if (ctx.path.compareFileFreshness(final, attempt.expected) !== TARGET_IS_NEW) {
|
|
116
|
+
return outcome(attempt, "deferred", "file-not-eligible");
|
|
117
|
+
}
|
|
118
|
+
const target = await ctx.host.storage().getStub(path);
|
|
119
|
+
if (!target || isFolderInfo(target) || !sameFileObservation(target, file)) {
|
|
120
|
+
progressAttempt(attempt, { file: target === null ? "absent" : "present", provenance: "unchanged" });
|
|
121
|
+
return outcome(attempt, "deferred", "storage-changed");
|
|
122
|
+
}
|
|
123
|
+
return await removeAndConfirm(attempt, record);
|
|
124
|
+
}
|
|
125
|
+
async function removeAndConfirm(attempt, record) {
|
|
126
|
+
const { ctx, path } = attempt;
|
|
127
|
+
progressAttempt(attempt, { phase: "remove", file: "unknown", provenance: "pending" });
|
|
128
|
+
try {
|
|
129
|
+
await ctx.host.storage().deleteVaultItem(path);
|
|
130
|
+
} catch (error) {
|
|
131
|
+
progressAttempt(attempt, { error, warning: "storage-post-removal-error" });
|
|
132
|
+
}
|
|
133
|
+
progressAttempt(attempt, { phase: "removal-verification" });
|
|
134
|
+
const current = await ctx.host.storage().getStub(path);
|
|
135
|
+
if (current !== null) {
|
|
136
|
+
progressAttempt(attempt, { file: "present", provenance: "unchanged", warning: void 0 });
|
|
137
|
+
return outcome(attempt, "failed", attempt.error ? "removal-failed" : "removal-no-effect");
|
|
138
|
+
}
|
|
139
|
+
progressAttempt(attempt, { file: "absent", provenance: "pending" });
|
|
140
|
+
return await clearAbsentProvenance(attempt, record);
|
|
141
|
+
}
|
|
142
|
+
async function clearAbsentProvenance(attempt, record) {
|
|
143
|
+
const { ctx, path } = attempt;
|
|
144
|
+
progressAttempt(attempt, { phase: "provenance-cleanup" });
|
|
145
|
+
if (!await isCurrentPathSelected(ctx, path)) return outcome(attempt, "deferred", "not-selected");
|
|
146
|
+
if (!await sameCurrentDeletion(attempt)) return outcome(attempt, "deferred", "database-changed");
|
|
147
|
+
const latest = await readProvenanceSnapshot(ctx, path, true);
|
|
148
|
+
if (!sameProvenance(latest, record)) return outcome(attempt, "deferred", "provenance-changed");
|
|
149
|
+
const target = await ctx.host.storage().getStub(path);
|
|
150
|
+
if (target !== null) {
|
|
151
|
+
progressAttempt(attempt, { file: "present", provenance: "unchanged" });
|
|
152
|
+
return outcome(attempt, "deferred", "file-recreated");
|
|
153
|
+
}
|
|
154
|
+
if (latest) await ctx.provenance.delete(getProvenancePath(ctx, path));
|
|
155
|
+
progressAttempt(attempt, { file: "absent", provenance: "cleared" });
|
|
156
|
+
return outcome(attempt, "applied", "deletion-confirmed");
|
|
157
|
+
}
|
|
158
|
+
async function sameCurrentDeletion({ ctx, expected, path }) {
|
|
159
|
+
const current = await ctx.host.db().fetchEntryMeta(path, void 0, true);
|
|
160
|
+
if (!current || !validDeletion(current)) return false;
|
|
161
|
+
if (current._id !== expected._id || current._rev !== expected._rev) return false;
|
|
162
|
+
if (ctx.host.getPath(current) !== path) return false;
|
|
163
|
+
if (await ctx.path.path2id(path) !== current._id) return false;
|
|
164
|
+
return (await ctx.host.db().getConflictedRevs(path)).length === 0;
|
|
165
|
+
}
|
|
166
|
+
function validDeletion(entry) {
|
|
167
|
+
return !!entry._id && !!entry._rev && entry.deleted === true && !entry._deleted && ["notes", "plain", "newnote"].includes(entry.type) && Array.isArray(entry.children) && Number.isFinite(entry.mtime) && Number.isFinite(entry.ctime) && Number.isSafeInteger(entry.size) && entry.size >= 0 && !entry._conflicts?.length;
|
|
168
|
+
}
|
|
169
|
+
function sameFileObservation(left, right) {
|
|
170
|
+
return !left.isInternal && !left.isFolder && left.path === right.path && left.stat.type === "file" && left.stat.mtime === right.stat.mtime && left.stat.ctime === right.stat.ctime && left.stat.size === right.stat.size;
|
|
171
|
+
}
|
|
172
|
+
function outcome(attempt, status, reason) {
|
|
173
|
+
const diagnostics = {
|
|
174
|
+
reason,
|
|
175
|
+
phase: attempt.phase,
|
|
176
|
+
...attempt.warning ? { warning: attempt.warning } : {},
|
|
177
|
+
...attempt.error ? { error: attempt.error } : {}
|
|
178
|
+
};
|
|
179
|
+
const { file, provenance } = attempt;
|
|
180
|
+
if (file === "absent" && provenance === "cleared" && status !== "deferred") {
|
|
181
|
+
return { ...diagnostics, status, file, provenance };
|
|
182
|
+
}
|
|
183
|
+
if (provenance === "unchanged" && status !== "applied") {
|
|
184
|
+
return { ...diagnostics, status, file, provenance };
|
|
185
|
+
}
|
|
186
|
+
if (provenance === "pending" && status !== "applied") {
|
|
187
|
+
if (file === "absent") return { ...diagnostics, status, file, provenance };
|
|
188
|
+
if (file === "unknown" && status === "failed") return { ...diagnostics, status, file, provenance };
|
|
189
|
+
}
|
|
190
|
+
throw new Error("Inconsistent deletion recovery outcome");
|
|
191
|
+
}
|
|
192
|
+
export {
|
|
193
|
+
recoverDatabaseDeletion
|
|
194
|
+
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { AnyEntry, FilePath, FilePathWithPrefix, MetaEntry, UXFileInfo, UXFileInfoStub, UXInternalFileInfoStub } from "../common/types.js";
|
|
2
|
-
import type { IFileHandler, StoreFileOptions } from "../interfaces/FileHandler.js";
|
|
2
|
+
import type { DeletionRecoveryResult, IFileHandler, StoreFileOptions } from "../interfaces/FileHandler.js";
|
|
3
3
|
import type { DatabaseFileAccess } from "../interfaces/DatabaseFileAccess.js";
|
|
4
4
|
import type { StorageAccess } from "../interfaces/StorageAccess.js";
|
|
5
5
|
import { ServiceModuleBase } from "./ServiceModuleBase.js";
|
|
@@ -61,6 +61,27 @@ export declare abstract class ServiceFileHandlerBase extends ServiceModuleBase<S
|
|
|
61
61
|
* @remarks Acquire the document lock while checking a metadata-only hint. No revision or file content is written; inspection failures are logged and return false.
|
|
62
62
|
*/
|
|
63
63
|
canSkipUnchangedFile(info: UXFileInfoStub, expected: MetaEntry): Promise<boolean>;
|
|
64
|
+
/**
|
|
65
|
+
* Check the host's explicit deletion-recovery capability without store access.
|
|
66
|
+
* @returns Whether complete provenance-key enumeration is available.
|
|
67
|
+
* @remarks This pure capability check never enumerates keys or reads provenance values.
|
|
68
|
+
*/
|
|
69
|
+
supportsDeletionRecovery(): boolean;
|
|
70
|
+
/**
|
|
71
|
+
* Enumerate the host's existing provenance keys for absent-file cleanup discovery.
|
|
72
|
+
* @returns Complete namespace-scoped provenance paths; host access errors propagate.
|
|
73
|
+
* @remarks Unsupported hosts reject. This operation reads keys without loading bodies or records.
|
|
74
|
+
*/
|
|
75
|
+
listDeletionRecoveryPaths(): Promise<FilePathWithPrefix[]>;
|
|
76
|
+
/**
|
|
77
|
+
* Recover one exact database deletion while preserving unproved or changed local content.
|
|
78
|
+
* @param expected - The current logical deletion selected by the ordinary scanner.
|
|
79
|
+
* @param cleanupOnly - Require an absent file and clear only stale device-local bookkeeping.
|
|
80
|
+
* @returns Separate observed file, provenance, and completion outcomes.
|
|
81
|
+
* @remarks The responsibility acquires a shared recovery permit before its document lock;
|
|
82
|
+
* verification failures are reported without changing normal incoming or save policy.
|
|
83
|
+
*/
|
|
84
|
+
recoverDatabaseDeletion(expected: MetaEntry, cleanupOnly?: boolean): Promise<DeletionRecoveryResult>;
|
|
64
85
|
/**
|
|
65
86
|
* Record an unchanged current revision after rechecking storage and database evidence.
|
|
66
87
|
* @param info - The supplied file observation or path.
|
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
} from "./ServiceFileHandlerBase/deletion.js";
|
|
11
11
|
import { renameFileInDB } from "./ServiceFileHandlerBase/renaming.js";
|
|
12
12
|
import { dbToStorageWithSpecificRev, dbToStorage } from "./ServiceFileHandlerBase/reflection.js";
|
|
13
|
+
import { recoverDatabaseDeletion } from "./ServiceFileHandlerBase/deletionRecovery.js";
|
|
13
14
|
import { createAllChunks } from "./ServiceFileHandlerBase/chunks.js";
|
|
14
15
|
import { ServiceModuleBase } from "./ServiceModuleBase.js";
|
|
15
16
|
import {} from "./ServiceFileHandlerBase/context.js";
|
|
@@ -135,6 +136,34 @@ class ServiceFileHandlerBase extends ServiceModuleBase {
|
|
|
135
136
|
async canSkipUnchangedFile(info, expected) {
|
|
136
137
|
return await canSkipUnchangedFile(this.context, info, expected);
|
|
137
138
|
}
|
|
139
|
+
/**
|
|
140
|
+
* Check the host's explicit deletion-recovery capability without store access.
|
|
141
|
+
* @returns Whether complete provenance-key enumeration is available.
|
|
142
|
+
* @remarks This pure capability check never enumerates keys or reads provenance values.
|
|
143
|
+
*/
|
|
144
|
+
supportsDeletionRecovery() {
|
|
145
|
+
return !!this.context.provenance?.listPaths;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Enumerate the host's existing provenance keys for absent-file cleanup discovery.
|
|
149
|
+
* @returns Complete namespace-scoped provenance paths; host access errors propagate.
|
|
150
|
+
* @remarks Unsupported hosts reject. This operation reads keys without loading bodies or records.
|
|
151
|
+
*/
|
|
152
|
+
async listDeletionRecoveryPaths() {
|
|
153
|
+
if (!this.context.provenance?.listPaths) throw new Error("Deletion recovery is unavailable for this host");
|
|
154
|
+
return await this.context.provenance.listPaths();
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Recover one exact database deletion while preserving unproved or changed local content.
|
|
158
|
+
* @param expected - The current logical deletion selected by the ordinary scanner.
|
|
159
|
+
* @param cleanupOnly - Require an absent file and clear only stale device-local bookkeeping.
|
|
160
|
+
* @returns Separate observed file, provenance, and completion outcomes.
|
|
161
|
+
* @remarks The responsibility acquires a shared recovery permit before its document lock;
|
|
162
|
+
* verification failures are reported without changing normal incoming or save policy.
|
|
163
|
+
*/
|
|
164
|
+
async recoverDatabaseDeletion(expected, cleanupOnly = false) {
|
|
165
|
+
return await recoverDatabaseDeletion(this.context, expected, cleanupOnly);
|
|
166
|
+
}
|
|
138
167
|
/**
|
|
139
168
|
* Record an unchanged current revision after rechecking storage and database evidence.
|
|
140
169
|
* @param info - The supplied file observation or path.
|
|
@@ -33,6 +33,15 @@ export declare abstract class KeyValueDBService<T extends ServiceContext = Servi
|
|
|
33
33
|
private _everyOnInitializeDatabase;
|
|
34
34
|
private _everyOnloadAfterLoadSettings;
|
|
35
35
|
constructor(context: T, dependencies: KeyValueDBDependencies<T>);
|
|
36
|
+
/**
|
|
37
|
+
* Create an inert namespaced handle over the service's current database.
|
|
38
|
+
* @param kind - Namespace identifier; stored keys receive this identifier and a trailing hyphen.
|
|
39
|
+
* @returns A handle whose operations resolve the current database when invoked.
|
|
40
|
+
* @remarks Construction performs no database I/O. Operations fail if the store
|
|
41
|
+
* is not ready. For keys, an undefined upper bound means the complete remaining
|
|
42
|
+
* namespace, including Unicode and empty keys. Explicit bounds are inclusive,
|
|
43
|
+
* and the optional count is passed to the database unchanged.
|
|
44
|
+
*/
|
|
36
45
|
openSimpleStore<T>(kind: string): {
|
|
37
46
|
get: (key: string) => Promise<T>;
|
|
38
47
|
set: (key: string, value: unknown) => Promise<void>;
|
|
@@ -101,6 +101,15 @@ class KeyValueDBService extends ServiceBase {
|
|
|
101
101
|
this.databaseEvents.onCloseDatabase.addHandler(this._onOtherDatabaseClose.bind(this));
|
|
102
102
|
}
|
|
103
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
* Create an inert namespaced handle over the service's current database.
|
|
106
|
+
* @param kind - Namespace identifier; stored keys receive this identifier and a trailing hyphen.
|
|
107
|
+
* @returns A handle whose operations resolve the current database when invoked.
|
|
108
|
+
* @remarks Construction performs no database I/O. Operations fail if the store
|
|
109
|
+
* is not ready. For keys, an undefined upper bound means the complete remaining
|
|
110
|
+
* namespace, including Unicode and empty keys. Explicit bounds are inclusive,
|
|
111
|
+
* and the optional count is passed to the database unchanged.
|
|
112
|
+
*/
|
|
104
113
|
openSimpleStore(kind) {
|
|
105
114
|
const getDB = () => {
|
|
106
115
|
if (!this._kvDB) {
|
|
@@ -120,8 +129,9 @@ class KeyValueDBService extends ServiceBase {
|
|
|
120
129
|
await getDB().del(`${prefix}${key}`);
|
|
121
130
|
},
|
|
122
131
|
keys: async (from, to, count) => {
|
|
132
|
+
const upperBound = to === void 0 ? `${prefix.slice(0, -1)}.` : `${prefix}${to || ""}`;
|
|
123
133
|
const ret = await getDB().keys(
|
|
124
|
-
IDBKeyRange.bound(`${prefix}${from || ""}`,
|
|
134
|
+
IDBKeyRange.bound(`${prefix}${from || ""}`, upperBound, false, to === void 0),
|
|
125
135
|
count
|
|
126
136
|
);
|
|
127
137
|
return ret.map((e) => (typeof e === "string" ? e : JSON.stringify(e)).toString()).filter((e) => e.startsWith(prefix)).map((e) => e.substring(prefix.length));
|
package/docs/development.md
CHANGED
|
@@ -53,6 +53,7 @@ not add consumer entry points.
|
|
|
53
53
|
| `provenance.ts`, `scanning.ts` | Track the displayed revision and content evidence, classify storage, revalidate snapshots, and inspect scan hints. |
|
|
54
54
|
| `saving.ts` | Save ordinary or reconciled storage content and handle explicit base-revision selection. |
|
|
55
55
|
| `deletion.ts`, `renaming.ts` | Apply deletion, revision removal, and source-preserving rename operations. |
|
|
56
|
+
| `deletionRecovery.ts` | Recover retained logical deletions and interrupted cleanup under opt-in provenance and shared file admission. |
|
|
56
57
|
| `reflection.ts`, `protection.ts` | Reflect database revisions while protecting unsynchronised storage content. |
|
|
57
58
|
| `events.ts`, `selection.ts` | Interpret ordinary, restored, retried, and replicated events against current selection and storage. |
|
|
58
59
|
| `chunks.ts` | Repair chunks through the host's save method with bounded concurrency. |
|
|
@@ -307,3 +308,6 @@ The shared value extraction uses the declared Octagonal Wheels dependency. Its
|
|
|
307
308
|
package's public duration and delimiter helpers; source tests cover the
|
|
308
309
|
internal JSON copy adapter. When testing a future unpublished dependency, keep
|
|
309
310
|
these checks tied to the same Octagonal Wheels tarball used by the downstream consumer.
|
|
311
|
+
|
|
312
|
+
The opt-in ordinary deletion recovery contract, runtime budgets, and restart
|
|
313
|
+
cleanup ownership are recorded in [Deletion recovery](deletion-recovery.md).
|
package/docs/offline-scanner.md
CHANGED
|
@@ -7,6 +7,12 @@ status: unreleased
|
|
|
7
7
|
|
|
8
8
|
# Full offline scan
|
|
9
9
|
|
|
10
|
+
The public entry remains `serviceFeatures/offlineScanner`. Internally,
|
|
11
|
+
`src/serviceFeatures/offlineScanner/` separates shared types, path comparison,
|
|
12
|
+
Metadata inspection and repair, last-seen persistence, file collection and
|
|
13
|
+
reflection, and scan orchestration. The entry module retains lifecycle integration
|
|
14
|
+
and re-exports its existing public functions; callers do not need to change imports.
|
|
15
|
+
|
|
10
16
|
Implementation note: the settings-controlled strict comparison correction for [Self-hosted LiveSync #1329](https://github.com/vrtmrz/obsidian-livesync/issues/1329) is implemented but unreleased. The original reviewed-version fields above retain their historical baseline.
|
|
11
17
|
|
|
12
18
|
This document defines the developer contract for `synchroniseAllFilesBetweenDBandStorage`. It describes how the scanner classifies each selected path, chooses an action, and records a database-to-storage reflection. Host-specific setup dialogue and recovery choices remain outside this contract.
|
|
@@ -179,3 +185,18 @@ Identity coverage verifies early quarantine when no consistent target exists, co
|
|
|
179
185
|
Stored-event coverage verifies that current storage content replaces saved observations, a recreated path suppresses a stale deletion, distinct-ID rename halves are admitted independently from current path state, and a same-ID rename never performs a separate deletion. It also verifies that a storage-inspection failure does not become evidence for a destructive operation, that failed work remains pending, and that an individual replay failure does not prevent the full scan from running.
|
|
180
186
|
|
|
181
187
|
Maintain that interaction boundary when changing file-handler results, last-seen persistence, Fast Setup reflection, or offline deletion detection.
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
## Opt-in recovery of retained logical deletions
|
|
191
|
+
|
|
192
|
+
The base action matrix remains the policy for hosts without deletion recovery.
|
|
193
|
+
A host whose handler reports `supportsDeletionRecovery()` may recover a
|
|
194
|
+
conflict-free, newer `both-db-deleted` pair during ordinary `NEWER_WINS` without
|
|
195
|
+
extra policies. This guarded operation does not use `update-storage` or direct
|
|
196
|
+
`delete-local`. Existing explicit policies retain their choices.
|
|
197
|
+
|
|
198
|
+
For `db-only-deleted` pairs, one optional provenance key enumeration identifies
|
|
199
|
+
stale bookkeeping. Cleanup-only work precedes new deletions and never reads file
|
|
200
|
+
bodies or invokes physical deletion. All affected paths are revalidated by their
|
|
201
|
+
handler. See [Deletion recovery](deletion-recovery.md) for completion, error,
|
|
202
|
+
concurrency, and cost contracts. This extension is unreleased.
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
date: 2026-10-26
|
|
3
3
|
commonlib-version: "0.2.1"
|
|
4
4
|
self-hosted-livesync-version: "1.0.40"
|
|
5
|
-
status:
|
|
5
|
+
status: published
|
|
6
6
|
---
|
|
7
7
|
|
|
8
8
|
# Storage events and database-to-storage reflection
|
|
@@ -11,8 +11,13 @@ This note separates storage-to-database event handling from
|
|
|
11
11
|
database-to-storage reflection. It documents the current guard against a stale
|
|
12
12
|
`DELETE` notification logically deleting Metadata while the same file is still
|
|
13
13
|
present in storage, and the accepted local completion contract for queued file
|
|
14
|
-
operations. The version fields identify the reviewed source versions
|
|
15
|
-
|
|
14
|
+
operations. The version fields identify the original reviewed source versions.
|
|
15
|
+
|
|
16
|
+
> Publication note (9th November, 2026): This implementation, including the
|
|
17
|
+
> [ordinary-file retry extension](#retrying-ordinary-file-operations), is included
|
|
18
|
+
> in published [Commonlib 0.4.1](https://github.com/vrtmrz/livesync-commonlib/releases/tag/0.4.1).
|
|
19
|
+
> The original review date and version baseline are retained above. Host
|
|
20
|
+
> applications adopt this package through their own dependency updates.
|
|
16
21
|
|
|
17
22
|
## Event-manager terminology
|
|
18
23
|
|
|
@@ -249,12 +254,13 @@ current content for processing.
|
|
|
249
254
|
|
|
250
255
|
## Retrying ordinary file operations
|
|
251
256
|
|
|
252
|
-
The following extension is
|
|
253
|
-
`DELETE`, or `RENAME` remains pending, while its attempt has
|
|
254
|
-
result. A later finite `commitPendingFileEvents()` request
|
|
255
|
-
attempt. A subsequent event for the same path or related rename
|
|
256
|
-
re-evaluate failed predecessors before proceeding. These paths
|
|
257
|
-
manager's ownership and use the existing current-state
|
|
257
|
+
The following extension is available in Commonlib 0.4.1. A failed ordinary
|
|
258
|
+
`CREATE`, `CHANGED`, `DELETE`, or `RENAME` remains pending, while its attempt has
|
|
259
|
+
a completed failure result. A later finite `commitPendingFileEvents()` request
|
|
260
|
+
can start a new attempt. A subsequent event for the same path or related rename
|
|
261
|
+
paths can also re-evaluate failed predecessors before proceeding. These paths
|
|
262
|
+
share the event manager's ownership and use the existing current-state
|
|
263
|
+
reconciliation rules.
|
|
258
264
|
|
|
259
265
|
Each request captures a finite set of operations and attempts. It may retry an
|
|
260
266
|
existing failure once, shares an attempt already running, and reports failures
|
|
@@ -286,7 +292,7 @@ Runtime retries reuse its file-reconciliation rules without replacing the
|
|
|
286
292
|
running manager through `restoreState()`. The saved format, handler return
|
|
287
293
|
contracts, and optional-provider retry policy remain unchanged. Hosts can use
|
|
288
294
|
their existing flush and lifecycle calls; no retry timer or new callback is
|
|
289
|
-
required. See the [retry decision](https://github.com/vrtmrz/obsidian-livesync/blob/
|
|
295
|
+
required. See the [reviewed retry decision](https://github.com/vrtmrz/obsidian-livesync/blob/09c17e90dde3b1a2000c8a2e13301245107b915b/docs/adr/2026_11_local_file_operation_retry.md)
|
|
290
296
|
for the operation matrix and considered alternatives.
|
|
291
297
|
|
|
292
298
|
## Optional file handlers
|
package/package.json
CHANGED