fix: align chunk reads with remote delivery

Consume commonlib's delivery lifecycle, classify finite replication entry points, and document why the five-minute inactivity fuse is only a leak safety valve. Record the remote-activity counter correction and preserve continuous replication's unbounded live channel.
This commit is contained in:
vorotamoroz
2026-07-15 17:39:39 +00:00
parent f54d162ef9
commit 9f9758bc12
16 changed files with 792 additions and 28 deletions
+1 -1
Submodule src/lib updated: a58965f9cd...4639e4537f
+5 -1
View File
@@ -150,7 +150,11 @@ Even if you choose to clean up, you will see this option again if you exit Obsid
await purgeUnreferencedChunks(this.localDatabase.localDatabase, false);
this.localDatabase.clearCaches();
// Perform the synchronisation once.
if (await this.core.replicator.openReplication(this.settings, false, showMessage, true)) {
const replicated = await this.services.replicator.runFiniteReplicationActivity(
() => this.core.replicator.openReplication(this.settings, false, showMessage, true),
{ label: "replication" }
);
if (replicated) {
await balanceChunkPurgedDBs(this.localDatabase.localDatabase, remoteDB.db);
await purgeUnreferencedChunks(this.localDatabase.localDatabase, false);
this.localDatabase.clearCaches();
@@ -69,6 +69,7 @@ describe("ModuleReplicator legacy cleanup", () => {
activityFinished();
}
});
const runFiniteReplicationActivity = vi.fn(async (task: () => unknown) => await task());
const openReplication = vi.fn(async () => true);
const activeReplicator = Object.assign(new LiveSyncCouchDBReplicator({} as any), {
connectRemoteCouchDBWithSetting: vi.fn(async () => ({ db: {} })),
@@ -90,6 +91,7 @@ describe("ModuleReplicator legacy cleanup", () => {
replicator: {
getActiveReplicator: vi.fn(() => activeReplicator),
runBoundedRemoteActivity,
runFiniteReplicationActivity,
},
};
const localDatabase = {
@@ -111,6 +113,9 @@ describe("ModuleReplicator legacy cleanup", () => {
expect(runBoundedRemoteActivity).toHaveBeenCalledWith(expect.any(Function), {
label: "database-cleanup",
});
expect(runFiniteReplicationActivity).toHaveBeenCalledWith(expect.any(Function), {
label: "replication",
});
expect(openReplication).toHaveBeenCalledOnce();
expect(openReplication.mock.invocationCallOrder[0]).toBeLessThan(activityFinished.mock.invocationCallOrder[0]);
expect(chunkMocks.balanceChunkPurgedDBs).toHaveBeenCalledOnce();
+1 -1
View File
@@ -33,7 +33,7 @@ export class ModuleReplicatorCouchDB extends AbstractModule {
if (continuous) {
void openReplication();
} else {
await this.services.replicator.runBoundedRemoteActivity(openReplication, {
await this.services.replicator.runFiniteReplicationActivity(openReplication, {
label: "replication",
});
}
@@ -3,7 +3,7 @@ import { ModuleReplicatorCouchDB } from "./ModuleReplicatorCouchDB.ts";
function createModule(settings: { liveSync: boolean; syncOnStart: boolean }, isReplicationReady = true) {
const openReplication = vi.fn(async () => true);
const runBoundedRemoteActivity = vi.fn(async (task: () => unknown) => await task());
const runFiniteReplicationActivity = vi.fn(async (task: () => unknown) => await task());
const services = {
API: {
addLog: vi.fn(),
@@ -20,7 +20,7 @@ function createModule(settings: { liveSync: boolean; syncOnStart: boolean }, isR
isReplicationReady: vi.fn(async () => isReplicationReady),
},
replicator: {
runBoundedRemoteActivity,
runFiniteReplicationActivity,
},
setting: {
saveSettingData: vi.fn(async () => undefined),
@@ -38,13 +38,13 @@ function createModule(settings: { liveSync: boolean; syncOnStart: boolean }, isR
return {
module: new ModuleReplicatorCouchDB(core),
openReplication,
runBoundedRemoteActivity,
runFiniteReplicationActivity,
};
}
describe("ModuleReplicatorCouchDB resume replication activity", () => {
it("runs start-up one-shot replication through bounded remote activity", async () => {
const { module, openReplication, runBoundedRemoteActivity } = createModule({
it("exposes start-up one-shot replication as finite replication activity", async () => {
const { module, openReplication, runFiniteReplicationActivity } = createModule({
liveSync: false,
syncOnStart: true,
});
@@ -52,14 +52,14 @@ describe("ModuleReplicatorCouchDB resume replication activity", () => {
await module._everyAfterResumeProcess();
await vi.waitFor(() => expect(openReplication).toHaveBeenCalledOnce());
expect(runBoundedRemoteActivity).toHaveBeenCalledWith(expect.any(Function), {
expect(runFiniteReplicationActivity).toHaveBeenCalledWith(expect.any(Function), {
label: "replication",
});
expect(openReplication).toHaveBeenCalledWith(expect.any(Object), false, false, false);
});
it("does not treat continuous replication as bounded activity", async () => {
const { module, openReplication, runBoundedRemoteActivity } = createModule({
it("does not wrap the unbounded continuous channel in another finite activity", async () => {
const { module, openReplication, runFiniteReplicationActivity } = createModule({
liveSync: true,
syncOnStart: false,
});
@@ -67,12 +67,12 @@ describe("ModuleReplicatorCouchDB resume replication activity", () => {
await module._everyAfterResumeProcess();
await vi.waitFor(() => expect(openReplication).toHaveBeenCalledOnce());
expect(runBoundedRemoteActivity).not.toHaveBeenCalled();
expect(runFiniteReplicationActivity).not.toHaveBeenCalled();
expect(openReplication).toHaveBeenCalledWith(expect.any(Object), true, false, false);
});
it("does not start a one-shot activity when start-up readiness fails", async () => {
const { module, openReplication, runBoundedRemoteActivity } = createModule(
const { module, openReplication, runFiniteReplicationActivity } = createModule(
{
liveSync: false,
syncOnStart: true,
@@ -83,7 +83,7 @@ describe("ModuleReplicatorCouchDB resume replication activity", () => {
await module._everyAfterResumeProcess();
await new Promise((resolve) => setTimeout(resolve, 0));
expect(runBoundedRemoteActivity).not.toHaveBeenCalled();
expect(runFiniteReplicationActivity).not.toHaveBeenCalled();
expect(openReplication).not.toHaveBeenCalled();
});
});
+1 -1
View File
@@ -87,7 +87,7 @@ export function useP2PReplicatorUI(
const activeReplicator = replicator.replicator;
if (!activeReplicator) return;
const settings = host.services.setting.currentSettings();
void host.services.replicator.runBoundedRemoteActivity(
void host.services.replicator.runFiniteReplicationActivity(
() => activeReplicator.openReplication(settings, false, true, false),
{ label: "replication" }
);
@@ -12,11 +12,11 @@ vi.mock("@/features/P2PSync/P2PReplicator/P2PServerStatusPaneView", () => ({
import { useP2PReplicatorUI } from "./useP2PReplicatorUI";
describe("useP2PReplicatorUI commands", () => {
it("runs a direct modal P2P replication command through the shared activity boundary", async () => {
it("exposes a direct modal P2P replication command as finite replication activity", async () => {
const commands: Array<{ id: string; checkCallback?: (isChecking: boolean) => unknown }> = [];
let initialise: (() => Promise<unknown>) | undefined;
const openReplication = vi.fn(async () => true);
const runBoundedRemoteActivity = vi.fn(async (task: () => unknown) => await task());
const runFiniteReplicationActivity = vi.fn(async (task: () => unknown) => await task());
const host = {
services: {
API: {
@@ -35,7 +35,7 @@ describe("useP2PReplicatorUI commands", () => {
onLayoutReady: { addHandler: vi.fn() },
},
setting: { currentSettings: vi.fn(() => ({ remoteType: "COUCHDB" })) },
replicator: { runBoundedRemoteActivity },
replicator: { runFiniteReplicationActivity },
},
} as any;
const p2p = {
@@ -51,7 +51,7 @@ describe("useP2PReplicatorUI commands", () => {
commands.find((command) => command.id === "replicate-now-by-p2p")?.checkCallback?.(false);
await vi.waitFor(() => expect(openReplication).toHaveBeenCalledOnce());
expect(runBoundedRemoteActivity).toHaveBeenCalledWith(expect.any(Function), {
expect(runFiniteReplicationActivity).toHaveBeenCalledWith(expect.any(Function), {
label: "replication",
});
});