Skip to content

armgit / ente

Public Imported from GitHub
0

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

Rearrange

Manav Rathi61b324ca051a
diff --git a/web/packages/new/photos/services/ml/cluster.ts b/web/packages/new/photos/services/ml/cluster.ts
index 5a376cd346..648ff5f842 100644
--- a/web/packages/new/photos/services/ml/cluster.ts
+++ b/web/packages/new/photos/services/ml/cluster.ts
@@ -113,12 +113,23 @@ export const clusterFaces = async (
// Get the locally available remote cluster groups.
const cgroupUserEntities = await savedCGroupUserEntities();
- // Sort them so that the latest ones are first. This is not expected to be
- // needed, it is just a safety check in case a client puts a face into two
- // clusters, in which case we want to use the more recent cgroup.
+
+ // Sort them so that the latest ones are first.
+ //
+ // This is not expected to be something that makes a functional difference
+ // but is done as part of a general theme of not making strict assumptions
+ // about the clusters we get from remote.
+ //
+ // In particular, the same face ID can be in different clusters. In such
+ // cases we should assign it arbitrarily assign it to the last cluster we
+ // find it in. Such leeway is intentionally provided to allow clients some
+ // slack in how they implement the sync without needing to make an blocking
+ // API request for every user interaction.
+
const sortedCGroupUserEntities = cgroupUserEntities.sort(
(a, b) => b.updatedAt - a.updatedAt,
);
+
// Extract the remote clusters.
clusters = clusters.concat(
sortedCGroupUserEntities.map((cg) => cg.data.assigned).flat(),
diff --git a/web/packages/new/photos/services/ml/index.ts b/web/packages/new/photos/services/ml/index.ts
index b4569b59e3..1e4b26ffd7 100644
--- a/web/packages/new/photos/services/ml/index.ts
+++ b/web/packages/new/photos/services/ml/index.ts
@@ -18,10 +18,14 @@ import { isInternalUser } from "../feature-flags";
import { getRemoteFlag, updateRemoteFlag } from "../remote-store";
import { setSearchPeople } from "../search";
import type { UploadItem } from "../upload/types";
-import { reconcileClusters } from "./cluster";
import { regenerateFaceCrops } from "./crop";
import { clearMLDB, getFaceIndex, getIndexableAndIndexedCounts } from "./db";
-import { namedPeopleFromCGroups, pullCGroups, type Person } from "./people";
+import {
+ pullCGroups,
+ reconstructPeople,
+ type NamedPerson,
+ type Person,
+} from "./people";
import { MLWorker } from "./worker";
import type { CLIPMatches } from "./worker-types";
@@ -83,20 +87,6 @@ class MLState {
*/
peopleSnapshot: Person[] | undefined;
- /**
- * Cached in-memory copy of people generated from local clusters.
- *
- * Part of {@link peopleSnapshot}.
- */
- peopleLocal: Person[] = [];
-
- /**
- * Cached in-memory copy of people generated from remote cgroups.
- *
- * Part of {@link peopleSnapshot}.
- */
- peopleRemote: Person[] = [];
-
/**
* In flight face crop regeneration promises indexed by the IDs of the files
* whose faces we are regenerating.
@@ -337,14 +327,7 @@ export const mlSync = async () => {
await wipCluster();
}
- // Update our in-memory snapshot of people.
- const namedPeople = await namedPeopleFromCGroups();
- _state.peopleRemote = namedPeople;
- updatePeopleSnapshot();
-
- // Notify the search subsystem of the update. Since the search only used
- // named cgroups, we only give it the people we got from cgroups.
- setSearchPeople(namedPeople);
+ await updatePeople();
_state.isSyncing = false;
};
@@ -386,12 +369,8 @@ export const wipCluster = async () => {
triggerStatusUpdate();
- const clusters = await worker().then((w) => w.clusterFaces());
- // TODO-Cluster this needs to happen in the worker itself
- await reconcileClusters(clusters);
+ await worker().then((w) => w.clusterFaces());
- _state.peopleLocal = [];
- updatePeopleSnapshot();
triggerStatusUpdate();
};
@@ -578,8 +557,19 @@ export const peopleSubscribe = (onChange: () => void): (() => void) => {
*/
export const peopleSnapshot = () => _state.peopleSnapshot;
-const updatePeopleSnapshot = () =>
- setPeopleSnapshot(_state.peopleRemote.concat(_state.peopleLocal));
+// Update our, and the search subsystem's, snapshot of people by reconstructing
+// it from the latest local state.
+const updatePeople = async () => {
+ const people = await reconstructPeople();
+
+ // Notify the search subsystem of the update (search only uses named ones).
+ setSearchPeople(
+ people.filter((p): p is NamedPerson => !!p.name),
+ );
+
+ // Update our in-memory list of people.
+ setPeopleSnapshot(people);
+};
const setPeopleSnapshot = (snapshot: Person[] | undefined) => {
_state.peopleSnapshot = snapshot;
diff --git a/web/packages/new/photos/services/ml/people.ts b/web/packages/new/photos/services/ml/people.ts
index 08fb77ddb8..2ba783c7b5 100644
--- a/web/packages/new/photos/services/ml/people.ts
+++ b/web/packages/new/photos/services/ml/people.ts
@@ -140,28 +140,20 @@ export type NamedPerson = Omit<Person, "name"> & {
};
/**
- * Construct in-memory {@link NamedPerson}s from the cgroups present locally.
+ * Construct in-memory people using the data present locally, ignoring faces
+ * belonging to deleted and hidden files.
*
* This function is meant to run after files, cgroups and faces have been synced
- * with remote. It then uses all the information in the local DBs to construct
- * an in-memory list of {@link Person}s on which the UI will operate.
+ * with remote, and clustering has completed. It uses the current local state to
+ * construct an in-memory list of {@link Person}s on which the UI will operate.
*
* @return A list of {@link Person}s, sorted by the number of files that they
* reference.
*/
-export const namedPeopleFromCGroups = async (): Promise<NamedPerson[]> => {
+export const reconstructPeople = async (): Promise<Person[]> => {
if (!process.env.NEXT_PUBLIC_ENTE_WIP_CL) return [];
if (!(await wipClusterEnable())) return [];
- // Ignore faces belonging to deleted (incl Trash) and hidden files.
- //
- // More generally, we should not make strict assumptions about the clusters
- // we get from remote. In particular, the same face ID can be in different
- // clusters. In such cases we should assign it arbitrarily assign it to the
- // last cluster we find it in. Such leeway is intentionally provided to
- // allow clients some slack in how they implement the sync without needing
- // to make an blocking API request for every user interaction.
-
const files = await getLocalFiles("normal");
const fileByID = new Map(files.map((f) => [f.id, f]));
@@ -182,7 +174,7 @@ export const namedPeopleFromCGroups = async (): Promise<NamedPerson[]> => {
// Convert cgroups to people.
const cgroups = await savedCGroupUserEntities();
- return cgroups
+ const namedPeople = cgroups
.map(({ id, data: cgroup }) => {
// Hidden cgroups are clusters specifically marked so as to not be shown
// in the UI.
@@ -234,6 +226,8 @@ export const namedPeopleFromCGroups = async (): Promise<NamedPerson[]> => {
})
.filter((c) => !!c)
.sort((a, b) => b.fileIDs.length - a.fileIDs.length);
+
+ return namedPeople.concat([]);
};
/**
diff --git a/web/packages/new/photos/services/ml/worker.ts b/web/packages/new/photos/services/ml/worker.ts
index 613aacb632..dfcbc60e5f 100644
--- a/web/packages/new/photos/services/ml/worker.ts
+++ b/web/packages/new/photos/services/ml/worker.ts
@@ -25,7 +25,7 @@ import {
indexCLIP,
type CLIPIndex,
} from "./clip";
-import { clusterFaces, type ClusteringProgress } from "./cluster";
+import { clusterFaces, reconcileClusters, type ClusteringProgress } from "./cluster";
import { saveFaceCrops } from "./crop";
import {
getFaceIndexes,
@@ -279,19 +279,22 @@ export class MLWorker {
}
/**
- * Run face clustering on all faces.
+ * Run face clustering on all faces, and update both local and remote state
+ * as appropriate.
*
* This should only be invoked when the face indexing (including syncing
- * with remote) is complete so that we cluster the latest set of faces.
+ * with remote) is complete so that we cluster the latest set of faces, and
+ * after we have fetched the latest cgroups from remote (so that we do no
+ * overwrite any remote updates).
*/
async clusterFaces() {
- const result = await clusterFaces(
+ const clusters = await clusterFaces(
await getFaceIndexes(),
await getAllLocalFiles(),
(progress) => this.updateClusteringProgress(progress),
);
this.updateClusteringProgress(undefined);
- return result;
+ await reconcileClusters(clusters)
}
private updateClusteringProgress(progress: ClusteringProgress | undefined) {
diff --git a/web/packages/new/photos/services/search/index.ts b/web/packages/new/photos/services/search/index.ts
index 07e3cd326d..a53a7fb4a5 100644
--- a/web/packages/new/photos/services/search/index.ts
+++ b/web/packages/new/photos/services/search/index.ts
@@ -4,7 +4,6 @@ import { ComlinkWorker } from "@/base/worker/comlink-worker";
import { FileType } from "@/media/file-type";
import i18n, { t } from "i18next";
import { clipMatches, isMLEnabled, isMLSupported } from "../ml";
-import type { NamedPerson } from "../ml/people";
import type {
LabelledFileType,
LabelledSearchDateComponents,
@@ -13,6 +12,7 @@ import type {
SearchSuggestion,
} from "./types";
import type { SearchWorker } from "./worker";
+import type { NamedPerson } from "../ml/people";
/**
* Cached instance of the {@link ComlinkWorker} that wraps our web worker.