Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Syncing starts once the device has read your whole notepad. Until then, nothing
- **Renames** carry across, including folder renames. A rename the plugin didn't see (Obsidian was closed) is recognised by the file's unchanged content. When you rename here with **Automatically update internal links** on, Obsidian's link edits sync like any other edit, and NOTE repairs only the links Obsidian never updates, those in `.mdx` notes, rather than repairing Obsidian's a second time. A rename Obsidian didn't see (made outside it, or while it was closed), or made with that setting off, has NOTE repair every link. A rename made in NOTE arrives with NOTE's link repair already done.
- **Deletions** carry across. In NOTE they go to NOTE's trash. Here they follow Obsidian's **Deleted files** setting: the system trash, the vault's `.trash` folder, or, if you chose to delete permanently, a permanent deletion, though NOTE's trash still has the file. An edit beats a deletion: if one side edited a file the other deleted, it comes back. Before a sync would delete more than fifty files, or more than ten that are also over a tenth of the vault, the plugin asks.
- **Swapped names** carry across too: two files that trade names, on either side, converge. While one waits for the other to leave its name, it may briefly sit beside it as `<name> moving <8 hex>.md`, and a sync that stops right then finishes the move next time.
- **Connection trouble:** when the connection to Flow ID fails, or Flow ID is busy with other uploads, nothing stops. Changes stay queued, the plugin's status says so (in the status bar, or in its settings on mobile), and sync tries again on its own: within 30 seconds, or within a minute when busy. Only a vault whose connection fails as it opens stops, and settings offer **Retry**.
- **Conflicts:** when both sides changed the same file, NOTE's version keeps the name and this vault's is saved beside it. A note's copy is named `<name> conflict <8 hex>.md`, the way NOTE names its own conflict copies; an attachment's keeps its extension last, `<name> conflict <8 hex>.png`, so Obsidian still opens it. Nothing is overwritten while you type: a file that changes during a sync, or an open note with unsaved typing, is left alone until the next sync. The same holds in NOTE: every write names the version it was planned from, so an edit that reaches NOTE during a sync is never replaced or trashed; the sync reads again and plans from there.

**Not synced:**
Expand Down
6 changes: 3 additions & 3 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "flow-note",
"name": "FLOW NOTE Sync",
"version": "0.1.2",
"version": "0.1.3",
"minAppVersion": "1.11.5",
"description": "Sync your vault both ways with your end-to-end encrypted NOTE notepad, linked once through your browser.",
"author": "Flow Industries",
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "flow-note-sync",
"private": true,
"version": "0.1.2",
"version": "0.1.3",
"description": "FLOW NOTE Sync for Obsidian",
"license": "MIT",
"type": "module",
Expand Down Expand Up @@ -30,6 +30,6 @@
"typescript-eslint": "8.72.1"
},
"dependencies": {
"@flow-industries/seal": "^0.3.1"
"@flow-industries/seal": "^0.4.0"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Regenerate the lockfile for Seal 0.4.0

This manifest now requires Seal 0.4.0, but bun.lock still declares and resolves ^0.3.1/0.3.1. Both CI and release run bun install --frozen-lockfile (.github/workflows/ci.yml:24 and .github/workflows/release.yml:29), and Bun documents that flag as “Disallow changes to lockfile,” so installation exits before any checks run; without frozen mode, the committed lock also lacks the new exports imported by this change. Regenerate and commit bun.lock for 0.4.0.

AGENTS.md reference: AGENTS.md:L5-L5

Useful? React with 👍 / 👎.

}
}
34 changes: 23 additions & 11 deletions src/controller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { type LinkedAccount, linkedAccount } from "./account";
import type { Custody } from "./custody";
import type {
DeviceOptions,
DeviceVault,
LinkOptions,
LinkPrompt,
NoteDeviceFetch,
TrustLoss,
import {
type DeviceOptions,
type DeviceVault,
failureWords,
type LinkOptions,
type LinkPrompt,
type NoteDeviceFetch,
type TrustLoss,
} from "./device";
import {
type Deletions,
Expand Down Expand Up @@ -207,7 +208,7 @@ export class SyncController {
} catch (error) {
if (!this.live(generation)) return;
this.close();
this.set({ kind: "stopped", message: messageOf(error) });
this.set({ kind: "stopped", message: stopMessageOf(error) });
}
}

Expand Down Expand Up @@ -455,7 +456,7 @@ export class SyncController {
if (!this.live(generation)) return;
this.close();
if (this.phase.kind !== "removed")
this.set({ kind: "stopped", message: messageOf(error) });
this.set({ kind: "stopped", message: stopMessageOf(error) });
} finally {
seed?.fill(0);
}
Expand Down Expand Up @@ -502,5 +503,16 @@ export class SyncController {
}
}

const messageOf = (error: unknown) =>
error instanceof Error ? error.message : String(error);
/** What the person reads for a failure: words for one Flow ID reported by code, else its own message. */
export const messageOf = (error: unknown) =>
failureWords(error) ??
(error instanceof Error ? error.message : String(error));

/** Said after a stop Flow ID caused: syncing stopped, but nothing here changed. */
export const KEPT = "Your changes are kept.";

/** What a stop says: one Flow ID caused also says the files here are kept. */
const stopMessageOf = (error: unknown) => {
const words = failureWords(error);
return words ? `${words} ${KEPT}` : messageOf(error);
};
44 changes: 44 additions & 0 deletions src/device.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import {
NoteChangedError,
parseSyncBackoff,
SYNC_BUSY,
SYNC_CONNECTION_FAILED,
SYNC_QUOTA_REACHED,
SYNC_TOO_LARGE,
SYNCED,
transientFailure,
} from "@flow-industries/seal";

/** The one place the plugin imports the NOTE device client from. */
Expand Down Expand Up @@ -47,3 +50,44 @@ export function followsRead(status: string): boolean {
refusal === SYNC_TOO_LARGE
);
}

export const CONNECTION_FAILED =
"The connection to Flow ID failed. Try again in a moment.";
export const UNAVAILABLE =
"Flow ID isn't available right now. Try again in a moment.";
export const BUSY =
"Flow ID is busy with other uploads from your account. Try again in a moment.";
export const REFUSED = "Flow ID couldn't complete that. Try again later.";

/** The code of a refusal Flow ID's SDK threw: a string `code` beside a numeric `status`. */
function refusalCode(error: unknown): string | undefined {
if (typeof error !== "object" || error === null) return undefined;
if (!("code" in error) || !("status" in error)) return undefined;
return typeof error.code === "string" && typeof error.status === "number"
? error.code
: undefined;
}

/**
* Words for a failure Flow ID reported by its code, so no code reaches the
* person; undefined for any other failure, which says what went wrong itself.
* A failed connection may be on either side, so its words blame neither.
* `PDS_UNAVAILABLE` is also what an error page without a code reads as, such
* as a gateway's during an outage.
*/
export function failureWords(error: unknown): string | undefined {
const transient = transientFailure(error);
if (transient === "connection") return CONNECTION_FAILED;
if (transient === "busy") return BUSY;
const code = refusalCode(error);
if (code === "PDS_UNAVAILABLE") return UNAVAILABLE;
return code === undefined ? undefined : REFUSED;
}

/** What the status bar says for a device status. A failed connection or a busy Flow ID reads as a wait, since the device retries on its own. */
export function statusWords(status: string): string {
if (status === SYNC_CONNECTION_FAILED)
return "Connection to Flow ID failed; changes are kept and sync resumes once it's back";
if (status === SYNC_BUSY) return "Syncing again in a moment";
return status;
}
5 changes: 2 additions & 3 deletions src/obsidian/modals.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type App, Modal, Notice, Setting } from "obsidian";
import { handleDomain, type LinkedAccount } from "../account";
import type { SyncController } from "../controller";
import { messageOf, type SyncController } from "../controller";
import type { LinkPrompt } from "../device";
import { linkPageFor } from "../link-page";

Expand Down Expand Up @@ -227,8 +227,7 @@ export class LinkModal extends Modal {
this.abort.signal,
);
} catch (error) {
if (!this.abort.signal.aborted)
this.error = error instanceof Error ? error.message : String(error);
if (!this.abort.signal.aborted) this.error = messageOf(error);
} finally {
this.abort = null;
this.render();
Expand Down
3 changes: 2 additions & 1 deletion src/obsidian/settings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { type App, type Plugin, PluginSettingTab, Setting } from "obsidian";
import type { Phase, SyncController } from "../controller";
import { statusWords } from "../device";
import type { RoundResult } from "../engine";
import { accountName, confirm } from "./modals";
import { custodyNote, updatesLinks } from "./vault";
Expand All @@ -23,7 +24,7 @@ export function statusText(phase: Phase, round: RoundResult | null): string {
const failed = round?.failed.length ?? 0;
const held = (round?.held.local ?? 0) + (round?.held.remote ?? 0);
return [
`FLOW NOTE: ${phase.status}`,
`FLOW NOTE: ${statusWords(phase.status)}`,
failed ? `${failed} not synced` : "",
held ? `${held} deletions held` : "",
]
Expand Down
58 changes: 58 additions & 0 deletions tests/controller.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { describe, expect, test } from "bun:test";
import {
SYNC_BUSY,
SYNC_CONNECTION_FAILED,
SYNC_PENDING,
SYNC_QUOTA_REACHED,
SYNC_RATE_LIMITED,
Expand All @@ -8,17 +10,24 @@ import {
} from "@flow-industries/seal";
import {
type ControllerOptions,
KEPT,
KEY_MISSING,
LOCKED_ELSEWHERE,
messageOf,
REMOVED,
STRANDED,
SyncController,
} from "../src/controller";
import { Custody, type SecretBackend } from "../src/custody";
import {
BUSY,
CONNECTION_FAILED,
type DeviceOptions,
type DeviceVault,
followsRead,
REFUSED,
statusWords,
UNAVAILABLE,
} from "../src/device";
import { sha256 } from "../src/hash";
import { isStale, MemoryIndex, MemoryRemote, MemoryVault } from "./fakes";
Expand Down Expand Up @@ -350,6 +359,55 @@ describe("controller", () => {
// A rate limit can refuse the read itself, so it never counts.
expect(followsRead(`${SYNC_RATE_LIMITED} · Next sync 12:00`)).toBe(false);
expect(followsRead(SYNC_PENDING)).toBe(false);
// Nor can a read that never got an answer, or a busy Flow ID's.
expect(followsRead(SYNC_CONNECTION_FAILED)).toBe(false);
expect(followsRead(SYNC_BUSY)).toBe(false);
});

test("a Flow ID refusal stops an open in words, never a code, and says the changes are kept", async () => {
for (const [code, status, words] of [
["CONNECTION_LOST", 0, CONNECTION_FAILED],
["UPLOAD_INCOMPLETE", 408, CONNECTION_FAILED],
["PDS_UNAVAILABLE", 503, UNAVAILABLE],
["UPLOAD_BUSY", 429, BUSY],
["RATE_LIMITED", 429, REFUSED],
["RECONNECT_REQUIRED", 403, REFUSED],
["DEVICE_LINKING_DISABLED", 403, REFUSED],
] as const) {
// How the device client throws a transport refusal: an Error whose message is the code.
const refusal = () => Object.assign(new Error(code), { code, status });
const h = await harness({
onOpen: () => {
throw refusal();
},
});
await h.controller.start();
const stopped = `${words} ${KEPT}`;
expect(h.controller.phase).toEqual({ kind: "stopped", message: stopped });
expect(h.notices).toEqual([`Syncing stopped. ${stopped}`]);
// A link has no changes in play, so the link modal's words leave that out.
expect(messageOf(refusal())).toBe(words);
expect(messageOf(refusal())).not.toContain(code);
}
expect(messageOf(new Error(LOCKED_ELSEWHERE))).toBe(LOCKED_ELSEWHERE);
// A code without a status isn't Flow ID's, so it keeps its own message.
expect(
messageOf(Object.assign(new Error("no such file"), { code: "ENOENT" })),
).toBe("no such file");
expect(messageOf("plain")).toBe("plain");
});

test("the status bar reads a failed connection or a busy Flow ID as a wait, and passes every other status through", () => {
expect(statusWords(SYNC_CONNECTION_FAILED)).toBe(
"Connection to Flow ID failed; changes are kept and sync resumes once it's back",
);
expect(statusWords(SYNC_BUSY)).toBe("Syncing again in a moment");
for (const status of [
SYNCED,
SYNC_PENDING,
`${SYNC_RATE_LIMITED} · Next sync 12:00`,
])
expect(statusWords(status)).toBe(status);
});

test("a close while the vault still opens keeps the lock until that open settles and stops", async () => {
Expand Down
58 changes: 56 additions & 2 deletions tests/e2e.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { afterEach, describe, expect, test } from "bun:test";
import { SYNC_CONNECTION_FAILED } from "@flow-industries/seal";
import * as kit from "@flow-industries/seal/internal/testing/kit";
import {
type AdminBrowser,
Expand All @@ -12,9 +13,10 @@ import {
TEST_DID,
TEST_HOST,
} from "@flow-industries/seal/testing";
import { REMOVED, SyncController } from "../src/controller";
import { KEPT, REMOVED, SyncController } from "../src/controller";
import { Custody, type SecretBackend } from "../src/custody";
import {
CONNECTION_FAILED,
createDeviceSeed,
type DeviceCheckpoint,
deleteDeviceStorage,
Expand Down Expand Up @@ -58,6 +60,8 @@ function obsidian(note: FakeFlowId, name: string) {
const notices: string[] = [];
/** Every mass deletion a round asked to confirm; each is approved, so a planned deletion would run. */
const asked: Deletions[] = [];
/** While down, no request reaches Flow ID: what a dropped network looks like to the device. */
const network = { down: false };
const controller: SyncController = new SyncController({
custody,
device: {
Expand All @@ -69,7 +73,10 @@ function obsidian(note: FakeFlowId, name: string) {
stale: isStaleWrite,
},
host: TEST_HOST,
fetch: (url, init) => note.fetch(url, init),
fetch: (url, init) =>
network.down
? Promise.reject(new TypeError("Failed to fetch"))
: note.fetch(url, init),
local,
configDir: ".obsidian",
updatesLinks: () => links.updated,
Expand Down Expand Up @@ -112,6 +119,7 @@ function obsidian(note: FakeFlowId, name: string) {
losses,
notices,
asked,
network,
turn,
};
}
Expand Down Expand Up @@ -288,6 +296,52 @@ describe("two Obsidian vaults synced through NOTE", () => {
expect(note.violations).toEqual([]);
}, 60_000);

test("a failed connection to Flow ID reads as a wait, never a code, and syncs once it's back", async () => {
const note = fakeFlowId();
const admin = await adminBrowser(note);
const a = obsidian(note, "dddddd");
opened.push(a);
a.local.put("Before.md", "synced first");
await link(admin, a);
await a.controller.confirm();
await a.controller.syncNow();

// Open: the device's own sync can't reach Flow ID, keeps the edit and says it will retry.
a.network.down = true;
a.local.put("Open.md", "written while open");
await a.controller.syncNow();
expect(a.controller.phase).toEqual({
kind: "open",
status: SYNC_CONNECTION_FAILED,
});
a.controller.close();

// Closed: an open that can't reach Flow ID stops in words.
a.local.put("Closed.md", "written while closed");
await a.controller.start();
expect(a.controller.phase).toEqual({
kind: "stopped",
message: `${CONNECTION_FAILED} ${KEPT}`,
});
expect(a.notices).toEqual([
`Syncing stopped. ${CONNECTION_FAILED} ${KEPT}`,
]);

a.network.down = false;
await a.controller.syncNow();
expect(a.controller.phase.kind).toBe("open");
await a.controller.syncNow();
const web = await openAdminVault(admin);
await web.sync();
expect((await web.read()).notes.map((item) => item.path).sort()).toEqual([
"Before.md",
"Closed.md",
"Open.md",
]);
web.lock();
expect(note.violations).toEqual([]);
}, 60_000);

test("an edit that reaches the device mid-round becomes a conflict copy, never overwritten", async () => {
const note = fakeFlowId();
const admin = await adminBrowser(note);
Expand Down
Loading