@mulmoclaude/core 1.11.0 → 1.13.0
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/assets/helps/error-recovery.md +98 -0
- package/dist/collection/core/project.d.ts +11 -0
- package/dist/collection/index.cjs +3 -2
- package/dist/collection/index.js +3 -3
- package/dist/collection/registry/server/index.cjs +3 -3
- package/dist/collection/registry/server/index.js +3 -3
- package/dist/collection/server/index.cjs +2 -2
- package/dist/collection/server/index.js +2 -2
- package/dist/collection-watchers/index.cjs +3 -3
- package/dist/collection-watchers/index.js +3 -3
- package/dist/{discovery-B4CZvrXR.js → discovery-D-ZJ6a6z.js} +39 -18
- package/dist/discovery-D-ZJ6a6z.js.map +1 -0
- package/dist/{discovery-NRy3tyUA.cjs → discovery-DAoMnBYM.cjs} +39 -18
- package/dist/discovery-DAoMnBYM.cjs.map +1 -0
- package/dist/{dist-pWpC-b04.cjs → dist-21pMsI1a.cjs} +45 -1
- package/dist/{dist-pWpC-b04.cjs.map → dist-21pMsI1a.cjs.map} +1 -1
- package/dist/{dist-Cwk0e12G.js → dist-DP1FTGnO.js} +16 -2
- package/dist/{dist-Cwk0e12G.js.map → dist-DP1FTGnO.js.map} +1 -1
- package/dist/feeds/server/engine.d.ts +27 -0
- package/dist/feeds/server/index.cjs +72 -7
- package/dist/feeds/server/index.cjs.map +1 -1
- package/dist/feeds/server/index.d.ts +1 -1
- package/dist/feeds/server/index.js +71 -8
- package/dist/feeds/server/index.js.map +1 -1
- package/dist/google/calendarStateLock.d.ts +23 -0
- package/dist/google/calendarSyncDue.d.ts +10 -0
- package/dist/google/calendarSyncStore.d.ts +20 -1
- package/dist/google/collectionProjection.d.ts +0 -8
- package/dist/google/collectionSync.d.ts +104 -15
- package/dist/google/index.cjs +512 -89
- package/dist/google/index.cjs.map +1 -1
- package/dist/google/index.d.ts +6 -3
- package/dist/google/index.js +501 -91
- package/dist/google/index.js.map +1 -1
- package/dist/notifier/index.cjs +1 -1
- package/dist/notifier/index.js +1 -1
- package/dist/notifier/store.d.ts +4 -4
- package/dist/{notifier-tMsAXyXp.cjs → notifier-C3IhYxqh.cjs} +39 -25
- package/dist/notifier-C3IhYxqh.cjs.map +1 -0
- package/dist/{notifier-BdA5qzhe.js → notifier-CMFlw74b.js} +39 -25
- package/dist/notifier-CMFlw74b.js.map +1 -0
- package/dist/project-B9NSyr6L.js +28 -0
- package/dist/project-B9NSyr6L.js.map +1 -0
- package/dist/project-CCtT_fQr.cjs +39 -0
- package/dist/project-CCtT_fQr.cjs.map +1 -0
- package/dist/remote-host/server/index.cjs +1 -1
- package/dist/remote-host/server/index.js +1 -1
- package/dist/remote-view/index.cjs +1 -1
- package/dist/remote-view/index.js +1 -1
- package/dist/scheduler/index.cjs +1 -1
- package/dist/scheduler/index.js +1 -1
- package/dist/{server-7U-3DE2e.cjs → server-B_b0bLSW.cjs} +3 -3
- package/dist/{server-7U-3DE2e.cjs.map → server-B_b0bLSW.cjs.map} +1 -1
- package/dist/{server-DdyIvFhz.js → server-Ci_A3chZ.js} +3 -3
- package/dist/{server-DdyIvFhz.js.map → server-Ci_A3chZ.js.map} +1 -1
- package/dist/utils/index.cjs +1 -1
- package/dist/utils/index.js +1 -1
- package/dist/whisper/index.cjs +1 -1
- package/dist/whisper/index.js +1 -1
- package/dist/wiki/index.cjs +1 -1
- package/dist/wiki/index.js +1 -1
- package/package.json +1 -1
- package/dist/discovery-B4CZvrXR.js.map +0 -1
- package/dist/discovery-NRy3tyUA.cjs.map +0 -1
- package/dist/notifier-BdA5qzhe.js.map +0 -1
- package/dist/notifier-tMsAXyXp.cjs.map +0 -1
- package/dist/project-BWI5w_BT.cjs +0 -17
- package/dist/project-BWI5w_BT.cjs.map +0 -1
- package/dist/project-bU98ycsy.js +0 -12
- package/dist/project-bU98ycsy.js.map +0 -1
package/dist/google/index.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import { t as errorMessage } from "../dist-
|
|
1
|
+
import { t as errorMessage } from "../dist-DP1FTGnO.js";
|
|
2
2
|
import { t as truncate } from "../text-CjDbP2O1.js";
|
|
3
3
|
import { t as writeFileAtomic } from "../atomic-DPpdrJzO.js";
|
|
4
4
|
import { fetchWithTimeout } from "../utils/fetch.js";
|
|
5
5
|
import { l as parseIsoDate, u as parseIsoDateTime } from "../calendarGrid-CrGxeCOG.js";
|
|
6
|
-
import {
|
|
6
|
+
import { t as mergeIntoExisting } from "../project-B9NSyr6L.js";
|
|
7
|
+
import { J as getWorkspaceRoot, d as storeFor, et as createForwardingLogger, n as discoverCollections, tt as createHostSlot } from "../discovery-D-ZJ6a6z.js";
|
|
7
8
|
import { promises } from "node:fs";
|
|
8
9
|
import path, { join } from "node:path";
|
|
9
|
-
import { randomBytes } from "node:crypto";
|
|
10
|
-
import { constants, copyFile, mkdir, readFile, readdir, rm, stat } from "node:fs/promises";
|
|
10
|
+
import { randomBytes, randomUUID } from "node:crypto";
|
|
11
|
+
import { constants, copyFile, mkdir, open, readFile, readdir, rm, stat, unlink } from "node:fs/promises";
|
|
11
12
|
import { homedir } from "node:os";
|
|
12
13
|
import { MISSED_RUN_POLICIES, SCHEDULE_TYPES } from "@receptron/task-scheduler";
|
|
13
14
|
import http from "node:http";
|
|
@@ -834,6 +835,161 @@ async function getCalendarColors(accessToken) {
|
|
|
834
835
|
};
|
|
835
836
|
}
|
|
836
837
|
//#endregion
|
|
838
|
+
//#region src/google/calendarStateLock.ts
|
|
839
|
+
var LOCK_MODE = 384;
|
|
840
|
+
/** How long a lock may sit untouched before another process may reclaim it.
|
|
841
|
+
*
|
|
842
|
+
* Sized against the HOLD time, which is one read plus one atomic write — a few
|
|
843
|
+
* milliseconds. Generous by three orders of magnitude so a slow disk never
|
|
844
|
+
* looks like a crash, while a host that died mid-write blocks the others for
|
|
845
|
+
* at most this long. */
|
|
846
|
+
var LOCK_STALE_MS = 1e4;
|
|
847
|
+
/** How long a caller waits before giving up and proceeding unlocked. */
|
|
848
|
+
var LOCK_WAIT_MS = 5e3;
|
|
849
|
+
var LOCK_RETRY_MS = 20;
|
|
850
|
+
var liveLockClock = {
|
|
851
|
+
now: () => Date.now(),
|
|
852
|
+
sleep: (delay_ms) => new Promise((resolve) => setTimeout(resolve, delay_ms))
|
|
853
|
+
};
|
|
854
|
+
var isEexist = (error) => typeof error === "object" && error !== null && "code" in error && error.code === "EEXIST";
|
|
855
|
+
/** The file's bytes, or null when it is not there. */
|
|
856
|
+
async function readLockRaw(lockPath) {
|
|
857
|
+
try {
|
|
858
|
+
return await readFile(lockPath, "utf-8");
|
|
859
|
+
} catch {
|
|
860
|
+
return null;
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
/** Absent means unheld; UNREADABLE does not — see `stealIfStale`. */
|
|
864
|
+
function parseLock(raw) {
|
|
865
|
+
try {
|
|
866
|
+
const parsed = JSON.parse(raw);
|
|
867
|
+
if (typeof parsed !== "object" || parsed === null) return null;
|
|
868
|
+
const { holder } = parsed;
|
|
869
|
+
return typeof holder === "string" ? { holder } : null;
|
|
870
|
+
} catch {
|
|
871
|
+
return null;
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
async function readLock(lockPath) {
|
|
875
|
+
const raw = await readLockRaw(lockPath);
|
|
876
|
+
return raw === null ? null : parseLock(raw);
|
|
877
|
+
}
|
|
878
|
+
/** Take the lock, or answer false because someone else holds a live one. */
|
|
879
|
+
async function tryAcquire(lockPath, holder) {
|
|
880
|
+
try {
|
|
881
|
+
const handle = await open(lockPath, "wx", LOCK_MODE);
|
|
882
|
+
try {
|
|
883
|
+
await handle.writeFile(JSON.stringify({ holder }));
|
|
884
|
+
} finally {
|
|
885
|
+
await handle.close();
|
|
886
|
+
}
|
|
887
|
+
return true;
|
|
888
|
+
} catch (error) {
|
|
889
|
+
if (!isEexist(error)) throw error;
|
|
890
|
+
return false;
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
/** How long this lock has existed, per the filesystem that holds it.
|
|
894
|
+
*
|
|
895
|
+
* From mtime rather than anything the holder wrote down, and for every lock
|
|
896
|
+
* rather than only the unreadable ones. A timestamp written by one host and
|
|
897
|
+
* compared against another host's clock is two wall clocks pretending to be
|
|
898
|
+
* one: skew either way reclaims a live lock or immortalises a dead one (Codex
|
|
899
|
+
* review #2690). Every host reading this workspace sees the SAME mtime.
|
|
900
|
+
*
|
|
901
|
+
* This still assumes the reader's clock and the filesystem's are within a TTL
|
|
902
|
+
* of each other — true by construction on a local disk, and a sub-second NTP
|
|
903
|
+
* question on a network mount, against a window of ten seconds.
|
|
904
|
+
*
|
|
905
|
+
* Reading mtime also covers the moment `open("wx")` has created the file but
|
|
906
|
+
* its payload has not landed yet: "unreadable" is a state every healthy lock
|
|
907
|
+
* passes through, and one being born reads as newborn rather than dead.
|
|
908
|
+
*
|
|
909
|
+
* Null when the file is gone, which is not an age but an absence. */
|
|
910
|
+
async function lockAgeMs(lockPath, clock) {
|
|
911
|
+
const stamped = await stat(lockPath).catch(() => null);
|
|
912
|
+
return stamped === null ? null : clock.now() - stamped.mtimeMs;
|
|
913
|
+
}
|
|
914
|
+
/** Drop a lock that has sat untouched past the TTL, whatever it says.
|
|
915
|
+
*
|
|
916
|
+
* Readable or not, nothing is reclaimed until it is demonstrably stale. For a
|
|
917
|
+
* readable one the holder is re-read and compared before the unlink, so a
|
|
918
|
+
* holder that renewed in between is not evicted. That narrows the window
|
|
919
|
+
* rather than closing it — two processes can still both decide to clear the
|
|
920
|
+
* same corpse. The cost of losing that race is one lost update, which is what
|
|
921
|
+
* this file reduces rather than a new failure, and reaching it takes a host
|
|
922
|
+
* dying mid-write plus a full TTL of waiting. */
|
|
923
|
+
async function stealIfStale(lockPath, clock) {
|
|
924
|
+
const raw = await readLockRaw(lockPath);
|
|
925
|
+
if (raw === null) return;
|
|
926
|
+
const held = parseLock(raw);
|
|
927
|
+
const age_ms = await lockAgeMs(lockPath, clock);
|
|
928
|
+
if (age_ms === null || age_ms < LOCK_STALE_MS) return;
|
|
929
|
+
if (held === null) {
|
|
930
|
+
log.warn("google", "reclaiming an unreadable calendar state lock", {
|
|
931
|
+
lockPath,
|
|
932
|
+
age_ms
|
|
933
|
+
});
|
|
934
|
+
await unlink(lockPath).catch(() => void 0);
|
|
935
|
+
return;
|
|
936
|
+
}
|
|
937
|
+
if ((await readLock(lockPath))?.holder !== held.holder) return;
|
|
938
|
+
log.warn("google", "reclaiming a calendar state lock whose holder went quiet", {
|
|
939
|
+
lockPath,
|
|
940
|
+
age_ms
|
|
941
|
+
});
|
|
942
|
+
await unlink(lockPath).catch(() => void 0);
|
|
943
|
+
}
|
|
944
|
+
async function release(lockPath, holder) {
|
|
945
|
+
if ((await readLock(lockPath))?.holder !== holder) return;
|
|
946
|
+
await unlink(lockPath).catch(() => void 0);
|
|
947
|
+
}
|
|
948
|
+
/** Run `mutate` with no other process mutating the same file.
|
|
949
|
+
*
|
|
950
|
+
* Fails OPEN. A workspace that cannot create the lock file — a read-only
|
|
951
|
+
* mount, a missing directory, an exhausted disk — still syncs, the way it did
|
|
952
|
+
* before #2679; the lock removes a race, it is not a precondition for
|
|
953
|
+
* correctness. The same applies to waiting the timeout out: proceeding
|
|
954
|
+
* unlocked risks the lost update this exists to prevent, whereas refusing
|
|
955
|
+
* would stop the sync outright.
|
|
956
|
+
*
|
|
957
|
+
* Note what this cannot promise. `O_EXCL` is atomic on a local filesystem and
|
|
958
|
+
* on NFSv3+, but a workspace living in a consumer sync folder (Dropbox,
|
|
959
|
+
* iCloud, Drive) has no such guarantee — there the file is replicated after
|
|
960
|
+
* the fact and both hosts believe they hold it. */
|
|
961
|
+
async function withCalendarStateLock(lockPath, mutate, clock = liveLockClock) {
|
|
962
|
+
const holder = randomUUID();
|
|
963
|
+
const deadline = clock.now() + LOCK_WAIT_MS;
|
|
964
|
+
let holding = false;
|
|
965
|
+
try {
|
|
966
|
+
holding = await acquireBefore(lockPath, holder, deadline, clock);
|
|
967
|
+
if (!holding) log.warn("google", "proceeding without the calendar state lock — another host is holding it", { lockPath });
|
|
968
|
+
return await mutate();
|
|
969
|
+
} finally {
|
|
970
|
+
if (holding) await release(lockPath, holder);
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
/** Poll until the lock is ours or the deadline passes. */
|
|
974
|
+
async function acquireBefore(lockPath, holder, deadline, clock) {
|
|
975
|
+
for (;;) {
|
|
976
|
+
try {
|
|
977
|
+
if (await tryAcquire(lockPath, holder)) return true;
|
|
978
|
+
await stealIfStale(lockPath, clock);
|
|
979
|
+
} catch (error) {
|
|
980
|
+
log.warn("google", "could not take the calendar state lock — mutating without it", {
|
|
981
|
+
lockPath,
|
|
982
|
+
error: String(error)
|
|
983
|
+
});
|
|
984
|
+
return false;
|
|
985
|
+
}
|
|
986
|
+
if (clock.now() >= deadline) return false;
|
|
987
|
+
await clock.sleep(LOCK_RETRY_MS);
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
/** `<state file>.lock`, so each state file is serialised on its own. */
|
|
991
|
+
var stateLockPath = (statePath) => `${statePath}.lock`;
|
|
992
|
+
//#endregion
|
|
837
993
|
//#region src/google/calendarSyncStore.ts
|
|
838
994
|
var SYNC_STATE_MODE = 384;
|
|
839
995
|
/** `<workspace>/data/calendar/.sync-state.json` */
|
|
@@ -841,34 +997,129 @@ function calendarSyncStatePath(workspaceRoot) {
|
|
|
841
997
|
return path.join(workspaceRoot ?? getWorkspaceRoot(), "data", "calendar", ".sync-state.json");
|
|
842
998
|
}
|
|
843
999
|
var calendarKey = canonicalCalendarId;
|
|
1000
|
+
/** Read one map out of whatever is on disk. Tolerant on purpose: a file written
|
|
1001
|
+
* before `lastSyncedAt` existed simply lacks the key, and a hand-edited one may
|
|
1002
|
+
* hold anything. A dropped entry costs a full re-walk or a duplicate run, never
|
|
1003
|
+
* data — throwing here would stop the sync entirely. */
|
|
1004
|
+
function stringRecord(value) {
|
|
1005
|
+
if (typeof value !== "object" || value === null) return {};
|
|
1006
|
+
return Object.fromEntries(Object.entries(value).filter((entry) => typeof entry[1] === "string"));
|
|
1007
|
+
}
|
|
844
1008
|
async function readState$1(workspaceRoot) {
|
|
845
1009
|
const stored = await readJsonOrNull(calendarSyncStatePath(workspaceRoot));
|
|
846
|
-
return
|
|
1010
|
+
return {
|
|
1011
|
+
tokens: stringRecord(stored?.tokens),
|
|
1012
|
+
lastSyncedAt: stringRecord(stored?.lastSyncedAt)
|
|
1013
|
+
};
|
|
847
1014
|
}
|
|
848
1015
|
var writeQueue$1 = { tail: Promise.resolve() };
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
1016
|
+
/** Decide from the stored state and write the result, both inside the queue, so
|
|
1017
|
+
* nothing in this process can read between the two. A `null` state skips the
|
|
1018
|
+
* write; `result` is what the caller learns about the decision. */
|
|
1019
|
+
async function updateStateWith(decide, workspaceRoot) {
|
|
1020
|
+
const statePath = calendarSyncStatePath(workspaceRoot);
|
|
1021
|
+
const run = writeQueue$1.tail.then(async () => await withCalendarStateLock(stateLockPath(statePath), async () => {
|
|
1022
|
+
const { state, result } = decide(await readState$1(workspaceRoot));
|
|
1023
|
+
if (state) await writeJsonAtomicWithMode(statePath, state, SYNC_STATE_MODE);
|
|
1024
|
+
return result;
|
|
1025
|
+
}));
|
|
854
1026
|
writeQueue$1.tail = run.catch(() => void 0);
|
|
855
1027
|
return await run;
|
|
856
1028
|
}
|
|
1029
|
+
async function updateState$1(mutate, workspaceRoot) {
|
|
1030
|
+
await updateStateWith((state) => ({
|
|
1031
|
+
state: mutate(state),
|
|
1032
|
+
result: void 0
|
|
1033
|
+
}), workspaceRoot);
|
|
1034
|
+
}
|
|
1035
|
+
var withoutKey = (record, dropped) => Object.fromEntries(Object.entries(record).filter(([key]) => key !== dropped));
|
|
857
1036
|
async function loadCalendarSyncToken(calendarId, workspaceRoot) {
|
|
858
1037
|
return (await readState$1(workspaceRoot)).tokens[calendarKey(calendarId)] ?? null;
|
|
859
1038
|
}
|
|
860
1039
|
async function saveCalendarSyncToken(calendarId, syncToken, workspaceRoot) {
|
|
861
1040
|
const stored = calendarKey(calendarId);
|
|
862
|
-
await updateState$1((
|
|
863
|
-
...
|
|
864
|
-
|
|
1041
|
+
await updateState$1((state) => ({
|
|
1042
|
+
...state,
|
|
1043
|
+
tokens: {
|
|
1044
|
+
...state.tokens,
|
|
1045
|
+
[stored]: syncToken
|
|
1046
|
+
}
|
|
865
1047
|
}), workspaceRoot);
|
|
866
1048
|
}
|
|
867
1049
|
/** Drop one calendar's token — used when Google answers 410, so the next run
|
|
868
|
-
* starts a clean full sync.
|
|
1050
|
+
* starts a clean full sync. Leaves the sync marker alone: "restart the walk"
|
|
1051
|
+
* says nothing about "nobody is running". */
|
|
869
1052
|
async function clearCalendarSyncToken(calendarId, workspaceRoot) {
|
|
870
1053
|
const dropped = calendarKey(calendarId);
|
|
871
|
-
await updateState$1((
|
|
1054
|
+
await updateState$1((state) => ({
|
|
1055
|
+
...state,
|
|
1056
|
+
tokens: withoutKey(state.tokens, dropped)
|
|
1057
|
+
}), workspaceRoot);
|
|
1058
|
+
}
|
|
1059
|
+
async function loadCalendarLastSyncedAt(calendarId, workspaceRoot) {
|
|
1060
|
+
return (await readState$1(workspaceRoot)).lastSyncedAt[calendarKey(calendarId)] ?? null;
|
|
1061
|
+
}
|
|
1062
|
+
/** Stamp when a sync of this calendar started, but only if `mayClaim` accepts
|
|
1063
|
+
* what the marker currently says. Answers whether THIS call is the one that
|
|
1064
|
+
* claimed it.
|
|
1065
|
+
*
|
|
1066
|
+
* The decision and the stamp share one pass through the queue on purpose.
|
|
1067
|
+
* Deciding from a snapshot read earlier — as a caller filtering a whole map of
|
|
1068
|
+
* calendars up front must — leaves both hosts believing the calendar is free,
|
|
1069
|
+
* which is the race this exists to close (Codex review #2680). What remains is
|
|
1070
|
+
* the read→write gap BETWEEN processes; closing that needs a real file lock
|
|
1071
|
+
* (#2679), and this stays a soft dedup until then.
|
|
1072
|
+
*
|
|
1073
|
+
* The time is passed in rather than read here so the rule stays testable
|
|
1074
|
+
* without freezing the clock. */
|
|
1075
|
+
async function claimCalendarSyncIfDue(calendarId, startedAt, mayClaim, workspaceRoot) {
|
|
1076
|
+
const stored = calendarKey(calendarId);
|
|
1077
|
+
return await updateStateWith((state) => {
|
|
1078
|
+
const claimed = mayClaim(state.lastSyncedAt[stored] ?? null);
|
|
1079
|
+
return {
|
|
1080
|
+
state: claimed ? {
|
|
1081
|
+
...state,
|
|
1082
|
+
lastSyncedAt: {
|
|
1083
|
+
...state.lastSyncedAt,
|
|
1084
|
+
[stored]: startedAt
|
|
1085
|
+
}
|
|
1086
|
+
} : null,
|
|
1087
|
+
result: claimed
|
|
1088
|
+
};
|
|
1089
|
+
}, workspaceRoot);
|
|
1090
|
+
}
|
|
1091
|
+
/** Drop one calendar's marker, so the next tick — on either host — may sync it
|
|
1092
|
+
* immediately instead of waiting the window out. */
|
|
1093
|
+
async function clearCalendarLastSyncedAt(calendarId, workspaceRoot) {
|
|
1094
|
+
const dropped = calendarKey(calendarId);
|
|
1095
|
+
await updateState$1((state) => ({
|
|
1096
|
+
...state,
|
|
1097
|
+
lastSyncedAt: withoutKey(state.lastSyncedAt, dropped)
|
|
1098
|
+
}), workspaceRoot);
|
|
1099
|
+
}
|
|
1100
|
+
//#endregion
|
|
1101
|
+
//#region src/google/calendarSyncDue.ts
|
|
1102
|
+
/** Slack between a tick and the marker it writes. Without it a lone host reads
|
|
1103
|
+
* its OWN marker as "not due yet" — elapsed lands a hair under the interval
|
|
1104
|
+
* every time — and syncs every other hour. */
|
|
1105
|
+
var TICK_JITTER_TOLERANCE_MS = 300 * 1e3;
|
|
1106
|
+
/** How long a fresh marker suppresses a sync: one interval, less the slack.
|
|
1107
|
+
* The slack is halved rather than subtracted flat for a very short configured
|
|
1108
|
+
* interval, where a fixed five minutes would swallow the window whole. */
|
|
1109
|
+
function calendarSyncDueWindowMs(intervalMs) {
|
|
1110
|
+
return intervalMs - Math.min(TICK_JITTER_TOLERANCE_MS, intervalMs / 2);
|
|
1111
|
+
}
|
|
1112
|
+
/** True iff no recent enough run stands in for this one.
|
|
1113
|
+
*
|
|
1114
|
+
* A marker that cannot be read as a time is treated as due: a stuck calendar is
|
|
1115
|
+
* worse than a duplicate run, which the sync is built to tolerate anyway
|
|
1116
|
+
* (writes are upserts by event id). */
|
|
1117
|
+
function isCalendarSyncDue(lastSyncedAt, windowMs, now = Date.now()) {
|
|
1118
|
+
if (!lastSyncedAt) return true;
|
|
1119
|
+
const elapsed = now - Date.parse(lastSyncedAt);
|
|
1120
|
+
if (!Number.isFinite(elapsed)) return true;
|
|
1121
|
+
if (elapsed < -windowMs) return true;
|
|
1122
|
+
return elapsed >= windowMs;
|
|
872
1123
|
}
|
|
873
1124
|
//#endregion
|
|
874
1125
|
//#region src/google/calendarPushState.ts
|
|
@@ -891,10 +1142,11 @@ async function readState(workspaceRoot) {
|
|
|
891
1142
|
}
|
|
892
1143
|
var writeQueue = { tail: Promise.resolve() };
|
|
893
1144
|
async function updateState(mutate, workspaceRoot) {
|
|
894
|
-
const
|
|
1145
|
+
const statePath = calendarPushStatePath(workspaceRoot);
|
|
1146
|
+
const run = writeQueue.tail.then(async () => await withCalendarStateLock(stateLockPath(statePath), async () => {
|
|
895
1147
|
const state = await readState(workspaceRoot);
|
|
896
|
-
await writeJsonAtomicWithMode(
|
|
897
|
-
});
|
|
1148
|
+
await writeJsonAtomicWithMode(statePath, { events: mutate(state.events) }, PUSH_STATE_MODE);
|
|
1149
|
+
}));
|
|
898
1150
|
writeQueue.tail = run.catch(() => void 0);
|
|
899
1151
|
return await run;
|
|
900
1152
|
}
|
|
@@ -1099,19 +1351,6 @@ function toCollectionRecord(event, map, primaryKey, fields) {
|
|
|
1099
1351
|
[primaryKey]: event.id
|
|
1100
1352
|
};
|
|
1101
1353
|
}
|
|
1102
|
-
/** Google's mapped values laid over whatever the record already holds.
|
|
1103
|
-
*
|
|
1104
|
-
* A record file is written whole (`writeItem`), so projecting alone would drop
|
|
1105
|
-
* every column the map does not name — a collection cannot then carry a local
|
|
1106
|
-
* note next to a mirrored event, because the next pull that touches that event
|
|
1107
|
-
* silently deletes it. The projection still wins on the fields it covers: those
|
|
1108
|
-
* are Google's to own. */
|
|
1109
|
-
function mergeIntoExisting(existing, projected) {
|
|
1110
|
-
return {
|
|
1111
|
-
...existing ?? {},
|
|
1112
|
-
...projected
|
|
1113
|
-
};
|
|
1114
|
-
}
|
|
1115
1354
|
//#endregion
|
|
1116
1355
|
//#region src/google/pushPlan.ts
|
|
1117
1356
|
/** The event fields Google lets a caller write. `htmlLink` and `status` are
|
|
@@ -1389,15 +1628,23 @@ function buildPatch(ctx, record, changed, shadow) {
|
|
|
1389
1628
|
}
|
|
1390
1629
|
};
|
|
1391
1630
|
}
|
|
1631
|
+
/** Google has dropped the event this record mirrors.
|
|
1632
|
+
*
|
|
1633
|
+
* The advice used to be "sync first, then re-create it", which sent people
|
|
1634
|
+
* nowhere: Google never resends a cancellation, so a sync sees nothing to do —
|
|
1635
|
+
* and before #2688 the sync that DID see it deleted the record along with the
|
|
1636
|
+
* edit. Deliberately silent on whether Google will hand the id back; that is
|
|
1637
|
+
* unverified. */
|
|
1638
|
+
var goneFromGoogle = (eventId) => ({
|
|
1639
|
+
kind: "skipped",
|
|
1640
|
+
message: `${eventId}: Google no longer has this event, and your local edit was kept. Delete this record, or copy it into a new one.`
|
|
1641
|
+
});
|
|
1392
1642
|
async function updateFromRecord(ctx, eventId, record, changed, shadow) {
|
|
1393
1643
|
const fetched = await getCalendarEvent(ctx.accessToken, {
|
|
1394
1644
|
calendarId: ctx.calendarId,
|
|
1395
1645
|
eventId
|
|
1396
1646
|
});
|
|
1397
|
-
if (fetched === null || fetched.event.status === "cancelled") return
|
|
1398
|
-
kind: "skipped",
|
|
1399
|
-
message: `${eventId}: the event no longer exists in Google — sync first, then re-create it`
|
|
1400
|
-
};
|
|
1647
|
+
if (fetched === null || fetched.event.status === "cancelled") return goneFromGoogle(eventId);
|
|
1401
1648
|
if (conflictingFields(shadow, toShadowEvent(fetched.event), changed).length > 0) return { kind: "conflict" };
|
|
1402
1649
|
const built = buildPatch(ctx, record, changed, shadow);
|
|
1403
1650
|
if (!built.ok) return {
|
|
@@ -1625,7 +1872,42 @@ function classifyDelete(eventId, kind) {
|
|
|
1625
1872
|
message: `delete ${eventId}: ${kind}`
|
|
1626
1873
|
};
|
|
1627
1874
|
}
|
|
1628
|
-
|
|
1875
|
+
/** Whether this record still says what the workspace last saw Google say.
|
|
1876
|
+
*
|
|
1877
|
+
* Compared against the baseline as it stood when this run STARTED READING, not
|
|
1878
|
+
* as it stands now: a full re-walk clears the baseline before writing a new one
|
|
1879
|
+
* (`restartFullSync`), so reading it live would answer "no baseline" for every
|
|
1880
|
+
* event and protect nothing exactly when the window is widest (#2684).
|
|
1881
|
+
*
|
|
1882
|
+
* A record with no baseline at all is NOT withheld — that is an event this
|
|
1883
|
+
* workspace has never held, so there is no local edit to lose. */
|
|
1884
|
+
function unsentEditGuard(schema, baseline) {
|
|
1885
|
+
const map = pushableMap(schema.googleCalendar?.map ?? {});
|
|
1886
|
+
return (existing, eventId) => {
|
|
1887
|
+
const shadow = baseline[eventId];
|
|
1888
|
+
if (shadow === void 0) return false;
|
|
1889
|
+
return locallyChangedFields(existing, baselineRecord(eventId, shadow, map, schema.primaryKey, schema.fields), map).length > 0;
|
|
1890
|
+
};
|
|
1891
|
+
}
|
|
1892
|
+
/** What the pull may do to the record behind one event.
|
|
1893
|
+
*
|
|
1894
|
+
* The guard runs BEFORE the status is consulted, and that order is the whole
|
|
1895
|
+
* fix: "is there something local to lose here?" outranks "what did Google do
|
|
1896
|
+
* to it?". Asking about the status first is how a cancellation kept deleting
|
|
1897
|
+
* records that held an edit Google had never seen (#2688), long after the
|
|
1898
|
+
* same guard had been put in front of the overwrite (#2684). */
|
|
1899
|
+
function applyPlanFor(existing, event, hasUnsentEdit) {
|
|
1900
|
+
if (existing !== null && hasUnsentEdit(existing, event.id)) return "withhold";
|
|
1901
|
+
return event.status === "cancelled" ? "delete" : "write";
|
|
1902
|
+
}
|
|
1903
|
+
/** Lay Google's mapped values over whatever the record already holds. Split out
|
|
1904
|
+
* so `applyEvent` reads as read → decide → act rather than carrying the write
|
|
1905
|
+
* itself (CodeRabbit review #2689). */
|
|
1906
|
+
async function writeProjected(write, event, schema, existing) {
|
|
1907
|
+
const record = toCollectionRecord(event, schema.googleCalendar?.map ?? {}, schema.primaryKey, schema.fields);
|
|
1908
|
+
return classifyWrite(event.id, (await write(event.id, mergeIntoExisting(existing, record))).kind);
|
|
1909
|
+
}
|
|
1910
|
+
async function applyEvent(collection, event, workspaceRoot, hasUnsentEdit) {
|
|
1629
1911
|
const { schema } = collection;
|
|
1630
1912
|
try {
|
|
1631
1913
|
const store = storeFor(collection, { workspaceRoot });
|
|
@@ -1633,13 +1915,11 @@ async function applyEvent(collection, event, workspaceRoot) {
|
|
|
1633
1915
|
kind: "unwritable",
|
|
1634
1916
|
message: `collection '${collection.slug}' is read-only`
|
|
1635
1917
|
};
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
const written = await store.write(event.id, mergeIntoExisting(await store.read(event.id), record));
|
|
1642
|
-
return classifyWrite(event.id, written.kind);
|
|
1918
|
+
const existing = await store.read(event.id);
|
|
1919
|
+
const plan = applyPlanFor(existing, event, hasUnsentEdit);
|
|
1920
|
+
if (plan === "withhold") return { kind: "withheld" };
|
|
1921
|
+
if (plan === "delete") return classifyDelete(event.id, (await store.delete(event.id)).kind);
|
|
1922
|
+
return await writeProjected(store.write, event, schema, existing);
|
|
1643
1923
|
} catch (error) {
|
|
1644
1924
|
return {
|
|
1645
1925
|
kind: "error",
|
|
@@ -1690,15 +1970,15 @@ var NOTHING_UNPUSHED = /* @__PURE__ */ new Set();
|
|
|
1690
1970
|
* protection — would overwrite the very edits this exists to protect; the read
|
|
1691
1971
|
* that failed is no evidence that the pull's own writes would fail too, so they
|
|
1692
1972
|
* would land (CodeRabbit review #2666). */
|
|
1693
|
-
var PROTECTION_UNKNOWN = "could not work out which records to protect
|
|
1694
|
-
/** What ONE collection's pull must leave alone: only what ITS OWN push
|
|
1973
|
+
var PROTECTION_UNKNOWN = "could not work out which records to protect from the pull";
|
|
1974
|
+
/** What ONE collection's pull must leave alone: only what ITS OWN push did not
|
|
1695
1975
|
* send.
|
|
1696
1976
|
*
|
|
1697
|
-
* Scoped per collection because a calendar can back several of them, and
|
|
1698
|
-
*
|
|
1699
|
-
* group starved a collection that never even declares `autoPush`:
|
|
1700
|
-
*
|
|
1701
|
-
*
|
|
1977
|
+
* Scoped per collection because a calendar can back several of them, and one
|
|
1978
|
+
* collection's unsent edit says nothing about the others. Sharing one set across
|
|
1979
|
+
* the group starved a collection that never even declares `autoPush`: a
|
|
1980
|
+
* neighbour's conflict froze its records — and the sync token still advanced, so
|
|
1981
|
+
* Google never resent them (Codex review #2666). */
|
|
1702
1982
|
var unpushedFor = (unpushed, slug) => {
|
|
1703
1983
|
const protection = unpushed.get(slug);
|
|
1704
1984
|
return protection === void 0 ? NOTHING_UNPUSHED : protection;
|
|
@@ -1723,16 +2003,19 @@ var allUnpushed = (unpushed) => new Set([...unpushed.values()].flatMap((ids) =>
|
|
|
1723
2003
|
* feature exists to prevent: a calendar whose role degrades to reader refuses
|
|
1724
2004
|
* the whole push, yet the pull still runs — reading needs no write access — and
|
|
1725
2005
|
* overwrote every unsent local edit while advancing its baseline past it, so the
|
|
1726
|
-
* next push could not even report the conflict (CodeRabbit review #2666).
|
|
2006
|
+
* next push could not even report the conflict (CodeRabbit review #2666). A
|
|
2007
|
+
* collection that never declares `autoPush` is in that same state on every
|
|
2008
|
+
* single run, which is how the loss reappeared without a failure in sight
|
|
2009
|
+
* (#2683).
|
|
1727
2010
|
*
|
|
1728
2011
|
* Protects the edited records rather than all of them, so an unchanged record
|
|
1729
2012
|
* keeps syncing normally. `null` when even that could not be worked out — the
|
|
1730
2013
|
* caller then refuses to pull the collection at all, because failing open here
|
|
1731
2014
|
* destroys exactly what this protects. */
|
|
1732
|
-
async function protectUnsentEdits(collection, workspaceRoot) {
|
|
2015
|
+
async function protectUnsentEdits(collection, workspaceRoot, deps) {
|
|
1733
2016
|
try {
|
|
1734
|
-
const edited = await
|
|
1735
|
-
if (edited.length > 0) log.warn("google", "protecting local edits
|
|
2017
|
+
const edited = await deps.unsentEdits(collection, workspaceRoot);
|
|
2018
|
+
if (edited.length > 0) log.warn("google", "protecting local edits that have not reached Google", {
|
|
1736
2019
|
slug: collection.slug,
|
|
1737
2020
|
edited
|
|
1738
2021
|
});
|
|
@@ -1745,8 +2028,18 @@ async function protectUnsentEdits(collection, workspaceRoot) {
|
|
|
1745
2028
|
return null;
|
|
1746
2029
|
}
|
|
1747
2030
|
}
|
|
1748
|
-
|
|
1749
|
-
|
|
2031
|
+
var livePullProtectionDeps = {
|
|
2032
|
+
pushNow: pushCollectionNow,
|
|
2033
|
+
unsentEdits: unsentLocalEdits
|
|
2034
|
+
};
|
|
2035
|
+
/** What ONE collection's pull must leave alone, pushing it first if it asked to
|
|
2036
|
+
* be pushed.
|
|
2037
|
+
*
|
|
2038
|
+
* A collection WITHOUT `autoPush` never pushes, so its local edits are unsent by
|
|
2039
|
+
* definition — the same state a failed push leaves behind, and it needs the same
|
|
2040
|
+
* protection. Pulling over them destroys the edit AND advances the baseline past
|
|
2041
|
+
* it, after which no conflict can be detected any more (#2683). "The push did not
|
|
2042
|
+
* run" is the condition that matters here; why it did not run is not.
|
|
1750
2043
|
*
|
|
1751
2044
|
* MUST run inside the calendar lock the caller already holds — hence
|
|
1752
2045
|
* `pushCollectionNow` rather than `pushCalendarForCollection`, which would take
|
|
@@ -1754,27 +2047,31 @@ async function protectUnsentEdits(collection, workspaceRoot) {
|
|
|
1754
2047
|
*
|
|
1755
2048
|
* A failed push must not stop the pull: the pull is what keeps the collection
|
|
1756
2049
|
* fresh, and a revoked write grant is no reason to freeze reading. */
|
|
1757
|
-
async function
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
const outcome = await
|
|
2050
|
+
async function pullProtectionFor(collection, workspaceRoot, deps = livePullProtectionDeps) {
|
|
2051
|
+
if (!collection.schema.googleCalendar?.autoPush) return await protectUnsentEdits(collection, workspaceRoot, deps);
|
|
2052
|
+
try {
|
|
2053
|
+
const outcome = await deps.pushNow(collection, workspaceRoot);
|
|
1761
2054
|
if (outcome.kind === "pushed") {
|
|
1762
2055
|
reportAutoPush(collection.slug, outcome.result);
|
|
1763
|
-
|
|
1764
|
-
continue;
|
|
2056
|
+
return new Set(outcome.result.unpushedIds);
|
|
1765
2057
|
}
|
|
1766
2058
|
log.warn("google", "auto push did not run", {
|
|
1767
2059
|
slug: collection.slug,
|
|
1768
2060
|
reason: outcome.kind
|
|
1769
2061
|
});
|
|
1770
|
-
unpushed.set(collection.slug, await protectUnsentEdits(collection, workspaceRoot));
|
|
1771
2062
|
} catch (error) {
|
|
1772
2063
|
log.warn("google", "auto push failed — pulling anyway", {
|
|
1773
2064
|
slug: collection.slug,
|
|
1774
2065
|
error: String(error)
|
|
1775
2066
|
});
|
|
1776
|
-
unpushed.set(collection.slug, await protectUnsentEdits(collection, workspaceRoot));
|
|
1777
2067
|
}
|
|
2068
|
+
return await protectUnsentEdits(collection, workspaceRoot, deps);
|
|
2069
|
+
}
|
|
2070
|
+
/** Push the `autoPush` collections in this group, and answer with what every
|
|
2071
|
+
* collection's pull must leave alone, keyed by collection. */
|
|
2072
|
+
async function pushAndProtect(collections, workspaceRoot, deps = livePullProtectionDeps) {
|
|
2073
|
+
const unpushed = /* @__PURE__ */ new Map();
|
|
2074
|
+
for (const collection of collections) unpushed.set(collection.slug, await pullProtectionFor(collection, workspaceRoot, deps));
|
|
1778
2075
|
return unpushed;
|
|
1779
2076
|
}
|
|
1780
2077
|
async function restartFullSync(accessToken, calendarId, workspaceRoot) {
|
|
@@ -1782,6 +2079,7 @@ async function restartFullSync(accessToken, calendarId, workspaceRoot) {
|
|
|
1782
2079
|
await clearCalendarShadow(calendarId, workspaceRoot);
|
|
1783
2080
|
return await syncCalendarEvents(accessToken, { calendarId });
|
|
1784
2081
|
}
|
|
2082
|
+
var ALWAYS_CLAIM = () => true;
|
|
1785
2083
|
/** Sync ONE calendar and fan its events out to every collection bound to it.
|
|
1786
2084
|
*
|
|
1787
2085
|
* The fan-out is not an optimisation, it is correctness: the sync token is
|
|
@@ -1796,21 +2094,80 @@ async function restartFullSync(accessToken, calendarId, workspaceRoot) {
|
|
|
1796
2094
|
* load the SAME stored token and walk the same window. That is idempotent —
|
|
1797
2095
|
* writes are upserts by event id — but it is a wasted full walk. Queued, the
|
|
1798
2096
|
* second pass resumes from the token the first just stored and fetches only
|
|
1799
|
-
* what is genuinely new.
|
|
1800
|
-
|
|
1801
|
-
|
|
2097
|
+
* what is genuinely new. That queue is module state, so it orders the doors
|
|
2098
|
+
* into THIS process only; `claimThenSync` is what other hosts can see. */
|
|
2099
|
+
async function syncCalendarGroup(calendarId, collections, workspaceRoot, mayClaim = ALWAYS_CLAIM) {
|
|
2100
|
+
return await withCalendarLock(calendarId, () => claimThenSync(calendarId, collections, workspaceRoot, mayClaim));
|
|
2101
|
+
}
|
|
2102
|
+
/** Take the workspace-shared marker BEFORE the calendar is touched, sync only if
|
|
2103
|
+
* this run got it, and drop it again if the run could not happen at all.
|
|
2104
|
+
*
|
|
2105
|
+
* Claiming first is the whole point: hosts tick in the same minute (interval
|
|
2106
|
+
* schedules align to wall-clock boundaries), so a marker written on completion
|
|
2107
|
+
* would leave the entire run — minutes, for a first full walk — open for a
|
|
2108
|
+
* second host to start alongside this one and lose the push baseline it writes.
|
|
2109
|
+
*
|
|
2110
|
+
* Released only when the sync could not run at all. A run that finished with
|
|
2111
|
+
* retryable per-collection errors KEEPS the marker: it already held the token
|
|
2112
|
+
* and the baseline back, so it replays on the next tick either way, and
|
|
2113
|
+
* releasing would hand a permanently-failing calendar back to both hosts at
|
|
2114
|
+
* once — the very overlap this guards (observed during Claude review). */
|
|
2115
|
+
async function claimThenSync(calendarId, collections, workspaceRoot, mayClaim) {
|
|
2116
|
+
if (!await claimCalendarSync(calendarId, workspaceRoot, mayClaim)) {
|
|
2117
|
+
log.info("google", "skipping a calendar this workspace synced recently — another host may be on it", { calendarId });
|
|
2118
|
+
return [];
|
|
2119
|
+
}
|
|
2120
|
+
try {
|
|
2121
|
+
return await syncCalendarGroupNow(calendarId, collections, workspaceRoot);
|
|
2122
|
+
} catch (error) {
|
|
2123
|
+
await releaseCalendarSyncClaim(calendarId, workspaceRoot);
|
|
2124
|
+
throw error;
|
|
2125
|
+
}
|
|
2126
|
+
}
|
|
2127
|
+
/** Never throws, and fails OPEN: the marker guards against duplicate work, it is
|
|
2128
|
+
* not a precondition for syncing correctly. A workspace that cannot store it
|
|
2129
|
+
* syncs the way it did before #2678. */
|
|
2130
|
+
async function claimCalendarSync(calendarId, workspaceRoot, mayClaim) {
|
|
2131
|
+
try {
|
|
2132
|
+
return await claimCalendarSyncIfDue(calendarId, (/* @__PURE__ */ new Date()).toISOString(), mayClaim, workspaceRoot);
|
|
2133
|
+
} catch (error) {
|
|
2134
|
+
log.warn("google", "could not stamp the calendar sync marker — another host may sync this calendar in parallel", {
|
|
2135
|
+
calendarId,
|
|
2136
|
+
error: String(error)
|
|
2137
|
+
});
|
|
2138
|
+
return true;
|
|
2139
|
+
}
|
|
2140
|
+
}
|
|
2141
|
+
async function releaseCalendarSyncClaim(calendarId, workspaceRoot) {
|
|
2142
|
+
try {
|
|
2143
|
+
await clearCalendarLastSyncedAt(calendarId, workspaceRoot);
|
|
2144
|
+
} catch (error) {
|
|
2145
|
+
log.warn("google", "could not release the calendar sync marker after a failed run", {
|
|
2146
|
+
calendarId,
|
|
2147
|
+
error: String(error)
|
|
2148
|
+
});
|
|
2149
|
+
}
|
|
1802
2150
|
}
|
|
2151
|
+
/** Every event whose baseline must NOT advance: what the push could not send,
|
|
2152
|
+
* plus what the apply refused to overwrite.
|
|
2153
|
+
*
|
|
2154
|
+
* The two must agree exactly. The apply's refusals only became visible after it
|
|
2155
|
+
* ran, so they join the union here rather than at push time — leaving them out
|
|
2156
|
+
* would advance the baseline past a record the pull deliberately left holding a
|
|
2157
|
+
* local edit, which is the silent overwrite this whole path exists to stop. */
|
|
2158
|
+
var heldBack = (unpushed, results) => /* @__PURE__ */ new Set([...allUnpushed(unpushed), ...results.flatMap((result) => result.withheld)]);
|
|
1803
2159
|
async function syncCalendarGroupNow(calendarId, collections, workspaceRoot) {
|
|
1804
|
-
const unpushed = await
|
|
2160
|
+
const unpushed = await pushAndProtect(collections, workspaceRoot);
|
|
2161
|
+
const baseline = await loadCalendarShadow(calendarId, workspaceRoot);
|
|
1805
2162
|
const accessToken = await getGoogleAccessToken();
|
|
1806
2163
|
const first = await syncCalendarEvents(accessToken, {
|
|
1807
2164
|
calendarId,
|
|
1808
2165
|
syncToken: await loadCalendarSyncToken(calendarId, workspaceRoot) ?? void 0
|
|
1809
2166
|
});
|
|
1810
2167
|
const result = first.fullResyncRequired ? await restartFullSync(accessToken, calendarId, workspaceRoot) : first;
|
|
1811
|
-
const results = await applyWindowToGroup(collections, result.events, workspaceRoot, unpushed);
|
|
2168
|
+
const results = await applyWindowToGroup(collections, result.events, workspaceRoot, unpushed, baseline);
|
|
1812
2169
|
if (windowFullyLanded(calendarId, results)) {
|
|
1813
|
-
await saveCalendarShadow(calendarId, shadowUpdates(result.events,
|
|
2170
|
+
await saveCalendarShadow(calendarId, shadowUpdates(result.events, heldBack(unpushed, results), baseline), workspaceRoot);
|
|
1814
2171
|
if (result.nextSyncToken) await advanceToken(calendarId, result.nextSyncToken, collections, workspaceRoot);
|
|
1815
2172
|
}
|
|
1816
2173
|
return results;
|
|
@@ -1819,7 +2176,7 @@ async function syncCalendarGroupNow(calendarId, collections, workspaceRoot) {
|
|
|
1819
2176
|
* one's own push protected. A collection whose protection could not be worked
|
|
1820
2177
|
* out is not pulled at all — it reports a retryable error instead, which holds
|
|
1821
2178
|
* the token and the baseline back for the whole group. */
|
|
1822
|
-
async function applyWindowToGroup(collections, events, workspaceRoot, unpushed) {
|
|
2179
|
+
async function applyWindowToGroup(collections, events, workspaceRoot, unpushed, baseline) {
|
|
1823
2180
|
const results = [];
|
|
1824
2181
|
for (const collection of collections) {
|
|
1825
2182
|
const protection = unpushedFor(unpushed, collection.slug);
|
|
@@ -1828,8 +2185,9 @@ async function applyWindowToGroup(collections, events, workspaceRoot, unpushed)
|
|
|
1828
2185
|
written: 0,
|
|
1829
2186
|
removed: 0,
|
|
1830
2187
|
unwritable: [],
|
|
2188
|
+
withheld: [],
|
|
1831
2189
|
errors: [PROTECTION_UNKNOWN]
|
|
1832
|
-
} : await applyEventsToCollection(collection, events, workspaceRoot, protection));
|
|
2190
|
+
} : await applyEventsToCollection(collection, events, workspaceRoot, protection, baseline));
|
|
1833
2191
|
}
|
|
1834
2192
|
return results;
|
|
1835
2193
|
}
|
|
@@ -1846,6 +2204,11 @@ function windowFullyLanded(calendarId, results) {
|
|
|
1846
2204
|
slug: entry.slug,
|
|
1847
2205
|
unwritable: entry.unwritable
|
|
1848
2206
|
}));
|
|
2207
|
+
results.filter((entry) => entry.withheld.length > 0).forEach((entry) => log.warn("google", "leaving calendar records alone — they hold edits Google has not seen", {
|
|
2208
|
+
calendarId,
|
|
2209
|
+
slug: entry.slug,
|
|
2210
|
+
withheld: entry.withheld
|
|
2211
|
+
}));
|
|
1849
2212
|
const failed = results.filter((entry) => entry.errors.length > 0);
|
|
1850
2213
|
failed.forEach((entry) => log.warn("google", "holding back calendar sync token after failed writes", {
|
|
1851
2214
|
calendarId,
|
|
@@ -1862,10 +2225,20 @@ function windowFullyLanded(calendarId, results) {
|
|
|
1862
2225
|
* keeps the local one would make the next push read a plain one-sided edit —
|
|
1863
2226
|
* no conflict to detect any more — and quietly overwrite Google. Held back, the
|
|
1864
2227
|
* baseline stays older than both sides, so the conflict keeps being reported
|
|
1865
|
-
* until someone resolves it (#2620).
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
2228
|
+
* until someone resolves it (#2620).
|
|
2229
|
+
*
|
|
2230
|
+
* `held` carries what those events must KEEP. Omitting them is enough on an
|
|
2231
|
+
* incremental run, where the file is merged rather than replaced — but a full
|
|
2232
|
+
* re-walk CLEARS the baseline first (`restartFullSync`), and there omission
|
|
2233
|
+
* drops the entry for good. The next push would then read a conflicted record
|
|
2234
|
+
* as a brand-new create, hit Google's duplicate-id 409 and refuse it, instead
|
|
2235
|
+
* of reporting the conflict it actually is. Re-stating the pre-run value makes
|
|
2236
|
+
* a held-back event behave the same either way (observed during Claude review;
|
|
2237
|
+
* no bot flagged it). */
|
|
2238
|
+
function shadowUpdates(events, unpushed = /* @__PURE__ */ new Set(), held = {}) {
|
|
2239
|
+
const advanced = pullableEvents(events, unpushed).map((event) => [event.id, event.status === "cancelled" ? null : toShadowEvent(event)]);
|
|
2240
|
+
const kept = [...unpushed].flatMap((eventId) => held[eventId] === void 0 ? [] : [[eventId, held[eventId]]]);
|
|
2241
|
+
return Object.fromEntries([...kept, ...advanced]);
|
|
1869
2242
|
}
|
|
1870
2243
|
/** Save the window's token unless every collection that consumed it was deleted
|
|
1871
2244
|
* while the sync was in flight.
|
|
@@ -1898,13 +2271,19 @@ async function pathExists(absPath) {
|
|
|
1898
2271
|
async function anySyncedCollectionSurvives(collections, exists = pathExists) {
|
|
1899
2272
|
return (await Promise.all(collections.map((collection) => exists(collection.skillDir)))).some(Boolean);
|
|
1900
2273
|
}
|
|
1901
|
-
async function applyEventsToCollection(collection, events, workspaceRoot, unpushed) {
|
|
1902
|
-
const
|
|
1903
|
-
|
|
2274
|
+
async function applyEventsToCollection(collection, events, workspaceRoot, unpushed, baseline) {
|
|
2275
|
+
const hasUnsentEdit = unsentEditGuard(collection.schema, baseline);
|
|
2276
|
+
const attempts = [];
|
|
2277
|
+
for (const event of pullableEvents(events, unpushed)) attempts.push({
|
|
2278
|
+
eventId: event.id,
|
|
2279
|
+
outcome: await applyEvent(collection, event, workspaceRoot, hasUnsentEdit)
|
|
2280
|
+
});
|
|
2281
|
+
const outcomes = attempts.map((attempt) => attempt.outcome);
|
|
1904
2282
|
return {
|
|
1905
2283
|
slug: collection.slug,
|
|
1906
2284
|
written: outcomes.filter((outcome) => outcome.kind === "written").length,
|
|
1907
2285
|
removed: outcomes.filter((outcome) => outcome.kind === "removed").length,
|
|
2286
|
+
withheld: attempts.flatMap((attempt) => attempt.outcome.kind === "withheld" ? [attempt.eventId] : []),
|
|
1908
2287
|
unwritable: outcomes.flatMap((outcome) => outcome.kind === "unwritable" ? [outcome.message] : []),
|
|
1909
2288
|
errors: outcomes.flatMap((outcome) => outcome.kind === "error" ? [outcome.message] : [])
|
|
1910
2289
|
};
|
|
@@ -1959,6 +2338,7 @@ async function releaseOrphanedCalendarToken(deleted, workspaceRoot) {
|
|
|
1959
2338
|
if (orphaned === null) return null;
|
|
1960
2339
|
await clearCalendarSyncToken(orphaned, workspaceRoot);
|
|
1961
2340
|
await clearCalendarShadow(orphaned, workspaceRoot);
|
|
2341
|
+
await clearCalendarLastSyncedAt(orphaned, workspaceRoot);
|
|
1962
2342
|
log.info("google", "cleared the sync token of a calendar no collection reads any more", { calendarId: orphaned });
|
|
1963
2343
|
return orphaned;
|
|
1964
2344
|
} catch (error) {
|
|
@@ -1975,19 +2355,27 @@ async function declaringGroups(workspaceRoot) {
|
|
|
1975
2355
|
* Authoring the collection before linking the account is an expected state,
|
|
1976
2356
|
* not a failure. Checking once here keeps it a quiet skip instead of an
|
|
1977
2357
|
* access-token throw per calendar, every hour, until the user links (#2188).
|
|
1978
|
-
* A user-triggered sync answers differently — it says so out loud.
|
|
2358
|
+
* A user-triggered sync answers differently — it says so out loud.
|
|
2359
|
+
*
|
|
2360
|
+
* Deliberately not phrased as a per-host state: the link lives in
|
|
2361
|
+
* `~/.config/mulmo`, which carries no app name, so every host on the machine
|
|
2362
|
+
* shares one answer. Reading it as "linked on this host only" is what made the
|
|
2363
|
+
* calendar look implicitly exclusive when it never was (#2678). */
|
|
1979
2364
|
async function backgroundSyncAllowed(groups) {
|
|
1980
2365
|
if (groups.size === 0) return false;
|
|
1981
2366
|
if (await isGoogleLinked()) return true;
|
|
1982
|
-
log.info("google", "skipping calendar sync — no Google account linked
|
|
2367
|
+
log.info("google", "skipping calendar sync — no Google account linked", { calendars: groups.size });
|
|
1983
2368
|
return false;
|
|
1984
2369
|
}
|
|
1985
2370
|
/** Run each group, isolating failures per calendar — one unreachable calendar
|
|
1986
|
-
* (or a revoked grant) must not stop the others.
|
|
1987
|
-
|
|
2371
|
+
* (or a revoked grant) must not stop the others. `mayClaim` is evaluated per
|
|
2372
|
+
* calendar as it comes up, never once for the whole map: an earlier calendar's
|
|
2373
|
+
* full walk takes minutes, and a decision made before it started says nothing
|
|
2374
|
+
* about who holds this one now. */
|
|
2375
|
+
async function runCalendarGroups(groups, workspaceRoot, mayClaim = ALWAYS_CLAIM) {
|
|
1988
2376
|
const results = [];
|
|
1989
2377
|
for (const [calendarId, collections] of groups) try {
|
|
1990
|
-
results.push(...await syncCalendarGroup(calendarId, collections, workspaceRoot));
|
|
2378
|
+
results.push(...await syncCalendarGroup(calendarId, collections, workspaceRoot, mayClaim));
|
|
1991
2379
|
} catch (error) {
|
|
1992
2380
|
log.warn("google", "calendar sync failed", {
|
|
1993
2381
|
calendarId,
|
|
@@ -1998,15 +2386,25 @@ async function runCalendarGroups(groups, workspaceRoot) {
|
|
|
1998
2386
|
written: 0,
|
|
1999
2387
|
removed: 0,
|
|
2000
2388
|
unwritable: [],
|
|
2389
|
+
withheld: [],
|
|
2001
2390
|
errors: [String(error)]
|
|
2002
2391
|
})));
|
|
2003
2392
|
}
|
|
2004
2393
|
return results;
|
|
2005
2394
|
}
|
|
2006
|
-
/** Sync every collection
|
|
2007
|
-
|
|
2395
|
+
/** Sync every collection whose calendar is due — no host in this workspace has
|
|
2396
|
+
* started one within `intervalMs` (#2678). Without that gate this walked every
|
|
2397
|
+
* declaring group on every tick, so a second host registering the same task
|
|
2398
|
+
* simply doubled the runs, concurrently.
|
|
2399
|
+
*
|
|
2400
|
+
* Dueness is not decided here, only described: the guard is handed down and
|
|
2401
|
+
* evaluated where the marker is written, so the answer cannot go stale between
|
|
2402
|
+
* deciding and claiming (Codex review #2680). */
|
|
2403
|
+
async function syncDueCalendarCollections(workspaceRoot, intervalMs = DEFAULT_SYNC_INTERVAL_MS) {
|
|
2008
2404
|
const groups = await declaringGroups(workspaceRoot);
|
|
2009
|
-
|
|
2405
|
+
if (!await backgroundSyncAllowed(groups)) return [];
|
|
2406
|
+
const windowMs = calendarSyncDueWindowMs(intervalMs);
|
|
2407
|
+
return await runCalendarGroups(groups, workspaceRoot, (lastSyncedAt) => isCalendarSyncDue(lastSyncedAt, windowMs));
|
|
2010
2408
|
}
|
|
2011
2409
|
/** The groups whose calendar has never synced. A missing token IS the "created
|
|
2012
2410
|
* since the last sync" signal — nothing else distinguishes a new collection
|
|
@@ -2052,10 +2450,21 @@ async function syncCalendarForCollection(slug, workspaceRoot, deps = liveManualS
|
|
|
2052
2450
|
results: await deps.runGroups(new Map(owning), workspaceRoot)
|
|
2053
2451
|
};
|
|
2054
2452
|
}
|
|
2453
|
+
/** The interval this task is scheduled on right now. A non-interval schedule
|
|
2454
|
+
* cannot be reached — the factory below builds an interval one — but the type
|
|
2455
|
+
* allows it, so it falls back rather than asserting. */
|
|
2456
|
+
function scheduledIntervalMs(task) {
|
|
2457
|
+
return task.schedule.type === SCHEDULE_TYPES.interval ? task.schedule.intervalMs : DEFAULT_SYNC_INTERVAL_MS;
|
|
2458
|
+
}
|
|
2055
2459
|
/** Scheduler registration, shaped like `feedRefreshTaskDef` so hosts wire it
|
|
2056
|
-
* with a single line.
|
|
2460
|
+
* with a single line.
|
|
2461
|
+
*
|
|
2462
|
+
* `run` reads the interval back off the definition instead of closing over the
|
|
2463
|
+
* option: a host rewrites `schedule` from its own overrides file AFTER this
|
|
2464
|
+
* returns, and the due window has to follow it. Frozen at the default, a
|
|
2465
|
+
* shortened interval would tick often and skip nearly every tick. */
|
|
2057
2466
|
function googleCalendarSyncTaskDef(opts) {
|
|
2058
|
-
|
|
2467
|
+
const def = {
|
|
2059
2468
|
id: GOOGLE_CALENDAR_SYNC_TASK_ID,
|
|
2060
2469
|
name: "Google Calendar sync",
|
|
2061
2470
|
description: "Pulls changed Google Calendar events into any collection declaring `googleCalendar`, without invoking the LLM.",
|
|
@@ -2064,8 +2473,9 @@ function googleCalendarSyncTaskDef(opts) {
|
|
|
2064
2473
|
intervalMs: opts?.intervalMs ?? DEFAULT_SYNC_INTERVAL_MS
|
|
2065
2474
|
},
|
|
2066
2475
|
missedRunPolicy: MISSED_RUN_POLICIES.runOnce,
|
|
2067
|
-
run: () => syncDueCalendarCollections(opts?.workspaceRoot ?? getWorkspaceRoot()).then(() => {})
|
|
2476
|
+
run: () => syncDueCalendarCollections(opts?.workspaceRoot ?? getWorkspaceRoot(), scheduledIntervalMs(def)).then(() => {})
|
|
2068
2477
|
};
|
|
2478
|
+
return def;
|
|
2069
2479
|
}
|
|
2070
2480
|
//#endregion
|
|
2071
2481
|
//#region src/google/tasks.ts
|
|
@@ -2262,6 +2672,6 @@ async function deleteDriveFile(accessToken, input) {
|
|
|
2262
2672
|
await googleRequest(DRIVE_API_LABEL, accessToken, `${DRIVE_FILES_URL}/${encodeURIComponent(input.fileId)}`, { method: "DELETE" });
|
|
2263
2673
|
}
|
|
2264
2674
|
//#endregion
|
|
2265
|
-
export { CANCELLED_EVENT_STATUS, DEFAULT_LIST_MAX_RESULTS, GOOGLE_AUTH_CANCELLED, GOOGLE_CALENDARLIST_SCOPE, GOOGLE_CALENDAR_SCOPE, GOOGLE_CALENDAR_SYNC_TASK_ID, GOOGLE_DRIVE_FILE_SCOPE, GOOGLE_SCOPES, GOOGLE_TASKS_SCOPE, GoogleApiError, HTTP_CONFLICT, HTTP_FORBIDDEN, HTTP_PRECONDITION_FAILED, MAX_LIST_RESULTS, PROTECTION_UNKNOWN, PUSHABLE_SOURCE_FIELDS, allUnpushed, anySyncedCollectionSurvives, assertSafeMimeType, authorizeGoogle, baselineRecord, brokerBaseUrl, brokerExchange, brokerRefresh, brokerStart, buildEventPatch, buildMultipartBody, buildTaskPatch, bySourceField, calendarApiError, calendarPushStatePath, calendarSyncStatePath, canonicalCalendarId, canonicalTaskListId, classifyDelete, classifyWrite, clearCalendarShadow, clearCalendarSyncToken, clientSecretPresence, collectCalendarPages, commitLinkedTokens, completeTask, configureGoogleHost, conflictingFields, createCalendarEvent, createDriveFile, createGoogleAuthFlow, createTask, deleteCalendarEvent, deleteDriveFile, deleteGoogleTokens, deleteTask, fieldText, findClientSecretPath, getCalendar, getCalendarColors, getCalendarEvent, getGoogleAccessToken, googleApiError, googleAuthFlow, googleCalendarSyncTaskDef, googleConfigDir, googleSecretsDir, googleTokenPath, groupByCalendar, isClientSettableEventId, isDeniedAccessRole, isGoogleApiError, isIsoDateTimeWithOffset, isTextMimeType, isUnpushed, legacyGoogleTokenPath, listCalendarEvents, listCalendars, listDriveFiles, listTaskLists, listTasks, loadCalendarShadow, loadCalendarSyncToken, loadClientSecret, loadGoogleTokens, locallyChangedFields, locallyDeletedIds, locallyEditedIds, mayAdoptExisting, mergeGoogleTokens, mergeIntoExisting, mergeShadow, orphanedCalendarId, pickBoundary, planRecord, pullableEvents, pushCalendarForCollection, pushCollectionNow, pushableMap, readDriveFile, releaseOrphanedCalendarToken, resolveEventSpan, saveCalendarShadow, saveCalendarSyncToken, saveGoogleTokens, shadowUpdates, syncCalendarEvents, syncCalendarForCollection, syncCalendarGroup, syncDueCalendarCollections, syncNewCalendarCollections, toCalendarSummary, toCollectionDateTime, toCollectionRecord, toDriveFileSummary, toEventSummary, toGoogleEventTime, toShadowEvent, toTaskListSummary, toTaskSummary, uncompleteTask, unlinkGoogle, unpushedFor, unsentLocalEdits, unsyncedGroups, updateCalendarEvent, updateTask, waitForAuthCode, withCalendarLock, withKeyedLock, zoneSuffixOf };
|
|
2675
|
+
export { CANCELLED_EVENT_STATUS, DEFAULT_LIST_MAX_RESULTS, GOOGLE_AUTH_CANCELLED, GOOGLE_CALENDARLIST_SCOPE, GOOGLE_CALENDAR_SCOPE, GOOGLE_CALENDAR_SYNC_TASK_ID, GOOGLE_DRIVE_FILE_SCOPE, GOOGLE_SCOPES, GOOGLE_TASKS_SCOPE, GoogleApiError, HTTP_CONFLICT, HTTP_FORBIDDEN, HTTP_PRECONDITION_FAILED, MAX_LIST_RESULTS, PROTECTION_UNKNOWN, PUSHABLE_SOURCE_FIELDS, allUnpushed, anySyncedCollectionSurvives, applyPlanFor, assertSafeMimeType, authorizeGoogle, baselineRecord, brokerBaseUrl, brokerExchange, brokerRefresh, brokerStart, buildEventPatch, buildMultipartBody, buildTaskPatch, bySourceField, calendarApiError, calendarPushStatePath, calendarSyncDueWindowMs, calendarSyncStatePath, canonicalCalendarId, canonicalTaskListId, claimCalendarSyncIfDue, classifyDelete, classifyWrite, clearCalendarLastSyncedAt, clearCalendarShadow, clearCalendarSyncToken, clientSecretPresence, collectCalendarPages, commitLinkedTokens, completeTask, configureGoogleHost, conflictingFields, createCalendarEvent, createDriveFile, createGoogleAuthFlow, createTask, deleteCalendarEvent, deleteDriveFile, deleteGoogleTokens, deleteTask, fieldText, findClientSecretPath, getCalendar, getCalendarColors, getCalendarEvent, getGoogleAccessToken, googleApiError, googleAuthFlow, googleCalendarSyncTaskDef, googleConfigDir, googleSecretsDir, googleTokenPath, groupByCalendar, heldBack, isCalendarSyncDue, isClientSettableEventId, isDeniedAccessRole, isGoogleApiError, isIsoDateTimeWithOffset, isTextMimeType, isUnpushed, legacyGoogleTokenPath, listCalendarEvents, listCalendars, listDriveFiles, listTaskLists, listTasks, liveLockClock, loadCalendarLastSyncedAt, loadCalendarShadow, loadCalendarSyncToken, loadClientSecret, loadGoogleTokens, locallyChangedFields, locallyDeletedIds, locallyEditedIds, mayAdoptExisting, mergeGoogleTokens, mergeIntoExisting, mergeShadow, orphanedCalendarId, pickBoundary, planRecord, pullProtectionFor, pullableEvents, pushAndProtect, pushCalendarForCollection, pushCollectionNow, pushableMap, readDriveFile, releaseOrphanedCalendarToken, resolveEventSpan, saveCalendarShadow, saveCalendarSyncToken, saveGoogleTokens, shadowUpdates, stateLockPath, syncCalendarEvents, syncCalendarForCollection, syncCalendarGroup, syncDueCalendarCollections, syncNewCalendarCollections, toCalendarSummary, toCollectionDateTime, toCollectionRecord, toDriveFileSummary, toEventSummary, toGoogleEventTime, toShadowEvent, toTaskListSummary, toTaskSummary, uncompleteTask, unlinkGoogle, unpushedFor, unsentEditGuard, unsentLocalEdits, unsyncedGroups, updateCalendarEvent, updateTask, waitForAuthCode, withCalendarLock, withCalendarStateLock, withKeyedLock, zoneSuffixOf };
|
|
2266
2676
|
|
|
2267
2677
|
//# sourceMappingURL=index.js.map
|