Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Handle versioning

Manav Rathiddf18bd0368b
diff --git a/web/apps/photos/src/services/face/crop.ts b/web/apps/photos/src/services/face/crop.ts
index ff1d6026af..ae5d44f104 100644
--- a/web/apps/photos/src/services/face/crop.ts
+++ b/web/apps/photos/src/services/face/crop.ts
@@ -1,6 +1,6 @@
+import type { Box } from "@/new/photos/services/face/types";
import { blobCache } from "@/next/blob-cache";
import type { FaceAlignment } from "./f-index";
-import type { Box } from "./types";
export const saveFaceCrop = async (
imageBitmap: ImageBitmap,
diff --git a/web/apps/photos/src/services/face/f-index.ts b/web/apps/photos/src/services/face/f-index.ts
index 584b733148..cf41623e94 100644
--- a/web/apps/photos/src/services/face/f-index.ts
+++ b/web/apps/photos/src/services/face/f-index.ts
@@ -1,5 +1,12 @@
import { FILE_TYPE } from "@/media/file-type";
import { decodeLivePhoto } from "@/media/live-photo";
+import type {
+ Box,
+ Dimensions,
+ Face,
+ Point,
+} from "@/new/photos/services/face/types";
+import { faceIndexingVersion } from "@/new/photos/services/face/types";
import type { EnteFile } from "@/new/photos/types/file";
import log from "@/next/log";
import { workerBridge } from "@/next/worker/worker-bridge";
@@ -21,7 +28,6 @@ import {
pixelRGBBilinear,
warpAffineFloat32List,
} from "./image";
-import type { Box, Dimensions, Face, Point } from "./types";
/**
* Index faces in the given file.
@@ -64,7 +70,7 @@ export const indexFaces = async (
width,
height,
faceEmbedding: {
- version: 1,
+ version: faceIndexingVersion,
client: userAgent,
faces: await indexFacesInBitmap(fileID, imageBitmap),
},
diff --git a/web/apps/photos/src/services/face/index.ts b/web/apps/photos/src/services/face/index.ts
deleted file mode 100644
index e69de29bb2..0000000000
diff --git a/web/apps/photos/src/services/face/indexer.worker.ts b/web/apps/photos/src/services/face/indexer.worker.ts
index 27608a31c8..9df8a455ef 100644
--- a/web/apps/photos/src/services/face/indexer.worker.ts
+++ b/web/apps/photos/src/services/face/indexer.worker.ts
@@ -3,12 +3,12 @@ import {
markIndexingFailed,
saveFaceIndex,
} from "@/new/photos/services/face/db";
+import type { FaceIndex } from "@/new/photos/services/face/types";
import type { EnteFile } from "@/new/photos/types/file";
import log from "@/next/log";
import { fileLogID } from "utils/file";
import { indexFaces } from "./f-index";
import { putFaceIndex } from "./remote";
-import type { FaceIndex } from "./types";
/**
* Index faces in a file, save the persist the results locally, and put them on
diff --git a/web/apps/photos/src/services/face/remote.ts b/web/apps/photos/src/services/face/remote.ts
index 2c4209ac28..daf177a0e4 100644
--- a/web/apps/photos/src/services/face/remote.ts
+++ b/web/apps/photos/src/services/face/remote.ts
@@ -1,8 +1,8 @@
+import type { FaceIndex } from "@/new/photos/services/face/types";
import type { EnteFile } from "@/new/photos/types/file";
import log from "@/next/log";
import ComlinkCryptoWorker from "@ente/shared/crypto";
import { putEmbedding } from "services/embeddingService";
-import type { FaceIndex } from "./types";
export const putFaceIndex = async (
enteFile: EnteFile,
diff --git a/web/packages/new/photos/services/embedding.ts b/web/packages/new/photos/services/embedding.ts
index c7f30afd8d..83d3b6d71a 100644
--- a/web/packages/new/photos/services/embedding.ts
+++ b/web/packages/new/photos/services/embedding.ts
@@ -1,11 +1,11 @@
import { getLocalTrashedFiles } from "@/new/photos/services/files";
import { authenticatedRequestHeaders } from "@/next/http";
-import { apiURL } from "@/next/origins";
import log from "@/next/log";
-import { nullToUndefined } from "@/utils/transform";
+import { apiURL } from "@/next/origins";
import { z } from "zod";
import { decryptFileMetadata } from "../../common/crypto/ente";
import { saveFaceIndex } from "./face/db";
+import { faceIndexingVersion, type FaceIndex } from "./face/types";
import { getAllLocalFiles } from "./files";
/**
@@ -22,17 +22,19 @@ import { getAllLocalFiles } from "./files";
*
* [Note: Handling versioning of embeddings]
*
- * The embeddings themselves have a version included in them, so it is possible
+ * The embeddings themselves have a version embedded in them, so it is possible
* for us to make backward compatible updates to the indexing process on newer
- * clients.
+ * clients (There is a top level version field too but that is not used.
*
* If we bump the version of same model (say when indexing on a newer client),
* the assumption will be that older client will be able to consume the
- * response. Say if we improve blur detection, older client should just consume
- * the newer version and not try to index the file locally.
+ * response. e.g. Say if we improve blur detection, older client should just
+ * consume embeddings with a newer version and not try to index the file again
+ * locally.
*
- * If you get version that is older, client should discard and try to index
- * locally (if needed) and also put the newer version it has on remote.
+ * If we get an embedding with version that is older than the version the client
+ * supports, then the client should ignore it. This way, the file will get
+ * reindexed locally an embedding with a newer version will get put to remote.
*
* In the case where the changes are not backward compatible and can only be
* consumed by clients with the relevant scaffolding, then we change this
@@ -65,12 +67,6 @@ const RemoteEmbedding = z.object({
decryptionHeader: z.string(),
/** Last time (epoch ms) this embedding was updated. */
updatedAt: z.number(),
- /**
- * The version for the embedding. Optional.
- *
- * See: [Note: Handling versioning of embeddings]
- */
- version: z.number().nullish().transform(nullToUndefined),
});
type RemoteEmbedding = z.infer<typeof RemoteEmbedding>;
@@ -86,7 +82,7 @@ type RemoteEmbedding = z.infer<typeof RemoteEmbedding>;
export const pullRemoteFaceEmbeddings = async () => {
const model: EmbeddingModel = "file-ml-clip-face";
const saveEmbedding = (jsonString: string) =>
- saveFaceIndex(FaceIndex.parse(JSON.parse(jsonString)));
+ saveFaceIndexIfNewer(FaceIndex.parse(JSON.parse(jsonString)));
// Include files from trash, otherwise they'll get unnecessarily reindexed
// if the user restores them from trash before permanent deletion.
@@ -160,6 +156,23 @@ const embeddingSyncTime = (model: EmbeddingModel) =>
const saveEmbeddingSyncTime = (t: number, model: EmbeddingModel) =>
localStorage.setItem("embeddingSyncTime:" + model, `${t}`);
+/**
+ * Save the given {@link faceIndex} locally if it is newer than the one we have.
+ *
+ * This is a variant of {@link saveFaceIndex} that performs version checking as
+ * described in [Note: Handling versioning of embeddings].
+ */
+export const saveFaceIndexIfNewer = async (index: FaceIndex) => {
+ const version = index.faceEmbedding.version;
+ if (version <= faceIndexingVersion) {
+ log.info(
+ `Ignoring remote face index with version ${version} not newer than what our indexer supports (${faceIndexingVersion})`,
+ );
+ return;
+ }
+ return saveFaceIndex(index);
+};
+
/**
* Zod schemas for the {@link FaceIndex} types.
*
diff --git a/web/packages/new/photos/services/face/types.ts b/web/packages/new/photos/services/face/types.ts
index a1db97a9af..44c78cc838 100644
--- a/web/packages/new/photos/services/face/types.ts
+++ b/web/packages/new/photos/services/face/types.ts
@@ -1,3 +1,8 @@
+/** The face indexing version supported by the current client. */
+// TODO: This belongs better to f-index.ts, but that file's in a different
+// package currently, move it there once these two files are together again.
+export const faceIndexingVersion = 1;
+
/**
* The faces in a file (and an embedding for each of them).
*