Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(google): note forward progress per imported contact and calendar event (#5349)

garrytanaecc46fa5757
diff --git a/src/core/google/google-source.ts b/src/core/google/google-source.ts
index 3266b3287e..8a27df36ef 100644
--- a/src/core/google/google-source.ts
+++ b/src/core/google/google-source.ts
@@ -248,7 +248,6 @@ interface GmailSweep {
activePack: ActivePack;
summary: GoogleSyncSummary;
countedSlugs: Set<string>;
- progressTick: (note: string) => void;
holds: ConnectorHoldSession;
}
@@ -262,7 +261,7 @@ type ThreadOutcome = { kind: 'landed'; newestMs: number } | { kind: 'gone' | 'sk
*/
async function attemptThread(g: GmailSweep, tid: string, version: string | null): Promise<ThreadOutcome> {
if (!g.holds.shouldAttempt(tid, version)) {
- g.progressTick(`thread ${tid} held`);
+ g.deps.tick(`thread ${tid} held`);
return { kind: 'skipped' };
}
const seen: { thread?: GmailThreadData } = {};
@@ -271,7 +270,7 @@ async function attemptThread(g: GmailSweep, tid: string, version: string | null)
g.holds.succeed(tid);
const newestMs = thread?.messages[thread.messages.length - 1]?.internalDateMs ?? 0;
if (newestMs > (g.state.gmail_newest_ms ?? 0)) g.state.gmail_newest_ms = newestMs;
- g.progressTick(`thread ${tid}`);
+ g.deps.tick(`thread ${tid}`);
return { kind: 'landed', newestMs };
} catch (e) {
if (e instanceof GoogleCursorExpiredError && e.status === 404) {
@@ -280,7 +279,7 @@ async function attemptThread(g: GmailSweep, tid: string, version: string | null)
// page it left behind.
g.holds.drop(tid);
g.deps.log(`[google] thread ${tid} vanished (404); skipping`);
- g.progressTick(`thread ${tid} gone`);
+ g.deps.tick(`thread ${tid} gone`);
return { kind: 'gone' };
}
const wasHeld = g.holds.isHeld(tid);
@@ -728,7 +727,9 @@ async function runGoogleSyncInner(engine: BrainEngine, sourceId: string, cfg: Go
const gmail = new GmailClient(...clientArgs);
const calendar = new CalendarClient(...clientArgs);
const people = new PeopleClient(...clientArgs);
- const deps: GoogleSyncDeps = { engine, sourceId, cfg, opts, entry, log, extractCandidates: [], managed, processedThreads: new Set() };
+ const progress = createProgress(cliOptsToProgressOptions(getCliOptions()));
+ const tick = (note: string): void => progress.tick(1, note);
+ const deps: GoogleSyncDeps = { engine, sourceId, cfg, opts, entry, log, tick, extractCandidates: [], managed, processedThreads: new Set() };
const summary: GoogleSyncSummary = {
status: 'synced',
@@ -787,9 +788,7 @@ async function runGoogleSyncInner(engine: BrainEngine, sourceId: string, cfg: Go
carryLegacyFailCounts(state.item_holds, state.gmail_fail_counts, (id) => id, new Date().toISOString()), { full: opts.full });
state.item_holds = holds.holds.initial();
delete state.gmail_fail_counts;
- const progress = createProgress(cliOptsToProgressOptions(getCliOptions()));
progress.start('sync.google_materialize');
- const tick = (note: string): void => progress.tick(1, note);
try {
const serviceErrors: string[] = [];
@@ -831,7 +830,7 @@ async function runGoogleSyncInner(engine: BrainEngine, sourceId: string, cfg: Go
// sweepGmail reports thread-level failures via its return value —
// they exit through normal returns, not throws, and stamping
// last_sync_at over them would blind the staleness gate (H1).
- gmailSweepOk = await sweepGmail({ deps, gmail, state, activePack, summary, countedSlugs, progressTick: tick, holds }, async () => {
+ gmailSweepOk = await sweepGmail({ deps, gmail, state, activePack, summary, countedSlugs, holds }, async () => {
// #5581: current mail is complete but the backfill is not: bank freshness now, while the managed lease can still write.
if (managed && summary.status !== 'partial' && !opts.signal?.aborted) {
await managed.saveState(state, true, new Date(state.gmail_newest_ms ?? Date.now()).toISOString());
diff --git a/src/core/google/people-calendar-sweep.ts b/src/core/google/people-calendar-sweep.ts
index 7cc1a5e8a0..3b026c7e5d 100644
--- a/src/core/google/people-calendar-sweep.ts
+++ b/src/core/google/people-calendar-sweep.ts
@@ -91,6 +91,7 @@ export async function sweepContacts(
await deletePageByRelPath(deps, existingPath, summary);
}
await importRendered(deps, rendered.relPath, rendered.markdown, activePack, summary, countedSlugs);
+ deps.tick(`contact ${c.resourceName}`);
}
// Cursor commits only after the whole sweep succeeded.
if (result.nextSyncToken) state.contacts_sync_token = result.nextSyncToken;
@@ -212,6 +213,7 @@ async function applyCalendarList(deps: GoogleSyncDeps, events: CalendarEventData
if (change.kind !== 'write' || !page) continue;
if (change.removeFirst) await deletePageByRelPath(deps, change.removeFirst, ctx.summary);
await importRendered(deps, page.relPath, page.markdown, ctx.activePack, ctx.summary, ctx.countedSlugs);
+ deps.tick(`event ${ev.id}`);
}
return !deps.opts.signal?.aborted;
}
diff --git a/src/core/google/sweep-shared.ts b/src/core/google/sweep-shared.ts
index 425924c208..9947dce3e1 100644
--- a/src/core/google/sweep-shared.ts
+++ b/src/core/google/sweep-shared.ts
@@ -52,6 +52,13 @@ export interface GoogleSyncDeps {
opts: SyncOpts;
entry: CredentialEntry;
log: (msg: string) => void;
+ /**
+ * #5349: one forward-progress note per imported item. The progress-aware
+ * sync deadline extends only on these (heartbeats are not progress), so a
+ * long first contacts or calendar sweep that is still landing pages is not
+ * killed at the hard deadline.
+ */
+ tick: (note: string) => void;
/** Threads whose newest message falls in the recent window — LLM
* extraction candidates, enqueued (capped) after the sweep. */
extractCandidates: Array<{ slug: string; threadId: string; newestMs: number }>;
diff --git a/test/google-source-reconcile.test.ts b/test/google-source-reconcile.test.ts
index b71721c951..2aee02f5a3 100644
--- a/test/google-source-reconcile.test.ts
+++ b/test/google-source-reconcile.test.ts
@@ -1631,3 +1631,58 @@ describe('loops_extract enqueue completeness', () => {
}
}, 120_000);
});
+
+// ── Sweep progress (#5349) ──────────────────────────────────────────────────
+
+/** Forward-progress notes recorded while `fn` runs (what the progress-aware sync deadline extends on). */
+async function countForwardProgress<T>(fn: () => Promise<T>): Promise<{ result: T; notes: number }> {
+ const { onForwardProgress } = await import('../src/core/forward-progress.ts');
+ let notes = 0;
+ const off = onForwardProgress(() => { notes++; });
+ try {
+ return { result: await fn(), notes };
+ } finally {
+ off();
+ }
+}
+
+function fxContact(n: number): unknown {
+ return {
+ resourceName: `people/c00000000${n}`,
+ names: [{ displayName: `Contact Example ${n}`, metadata: { primary: true } }],
+ emailAddresses: [{ value: `contact${n}@example.com` }],
+ };
+}
+
+describe('sweep progress (#5349)', () => {
+ test('a contacts sweep notes forward progress once per imported contact, so the progress-aware deadline extends', async () => {
+ const dir = mkdtempSync(join(tmpdir(), 'gsrc-pplprog-'));
+ const fx = emptyFx();
+ const vault = makeVault();
+ fx.contacts = [fxContact(1), fxContact(2), fxContact(3)];
+ try {
+ await insertGoogleSource(dir);
+ await withHome(async () => {
+ const { result, notes } = await countForwardProgress(() => sweep(dir, fx, vault, {}, 'contacts'));
+ expect(result.added).toBe(3);
+ expect(notes).toBeGreaterThanOrEqual(3);
+ });
+ } finally {
+ rmSync(dir, { recursive: true, force: true });
+ }
+ });
+
+ test('a calendar sweep notes forward progress once per listed event, and a list with nothing to import notes none', async () => {
+ await withCalendarSource(async ({ dir, fx, vault }) => {
+ fx.calendarEvents = [1, 2, 3, 4].map((n) => meeting(`evt0000000000000${n}`, n));
+ const first = await countForwardProgress(() => sweep(dir, fx, vault, {}, 'calendar'));
+ expect(first.result.added).toBe(4);
+ expect(first.notes).toBeGreaterThanOrEqual(4);
+ // Heartbeats are not progress: an empty delta must not extend the deadline.
+ fx.calendarDelta = [];
+ const second = await countForwardProgress(() => sweep(dir, fx, vault, {}, 'calendar'));
+ expect(second.result.status).toBe('up_to_date');
+ expect(second.notes).toBe(0);
+ });
+ });
+});