Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix: pre-landing review fixes (granted round)
garrytan55800eba38ea
diff --git a/docs/guides/google-connect.md b/docs/guides/google-connect.mdindex 6dcda89aa0..9f794cfbf7 100644--- a/docs/guides/google-connect.md+++ b/docs/guides/google-connect.md@@ -511,10 +511,14 @@ own cursor without restarting Gmail. A full contacts list (first sync, anexpired token, `--full`) saves its page cursor after each page and lists atmost 10 pages (2,000 contacts) per sync, so a large address book finishesover several syncs, each resuming where the last stopped, while Calendar and-Gmail still run every time. A contacts HTTP 400 on a sync-token request that+Gmail still run every time. A contact that keeps failing does not hold the+list back: the cursor moves past its page, and once the list ends the next+sync starts it again from page 1 without saving a sync token, so the contact+is retried. A contacts HTTP 400 on a sync-token request thatis not an expiry (a token minted under other request parameters) gets onefull-list attempt, and a saved page cursor Google no longer accepts (HTTP 400-or 410) restarts the list from page 1 once; every other HTTP 400 remains an+or 410, the 400 naming the page token) restarts the list from page 1 once;+every other HTTP 400 remains anupstream failure rather than triggering a re-list.## Custody, privacy, spenddiff --git a/src/core/google/google-clients.ts b/src/core/google/google-clients.tsindex f48e0df149..a77af045ca 100644--- a/src/core/google/google-clients.ts+++ b/src/core/google/google-clients.ts@@ -174,7 +174,8 @@ export class GoogleApiClient {// #8906: a connections pageToken Google no longer honors (a re-list resumed from its checkpoint).if (res.status === 400 && apiHint === 'people') {const u = new URL(url);- if (u.pathname.endsWith('/people/me/connections') && u.searchParams.get('pageToken')) throw new GoogleCursorExpiredError(res.status, url);+ const pageTokenRejected = /page[ _]?token/i.test(`${body.error?.message ?? ''} ${JSON.stringify(body.error?.details ?? [])}`);+ if (u.pathname.endsWith('/people/me/connections') && u.searchParams.get('pageToken') && pageTokenRejected) throw new GoogleCursorExpiredError(res.status, url);}throw new CredentialError('upstream', `: HTTP ${res.status} on ${apiHint} (${body.error?.message ?? 'no detail'})`);}diff --git a/src/core/google/people-calendar-sweep.ts b/src/core/google/people-calendar-sweep.tsindex 7146bb2f34..fcd68fdce5 100644--- a/src/core/google/people-calendar-sweep.ts+++ b/src/core/google/people-calendar-sweep.ts@@ -53,14 +53,16 @@ export async function sweepContacts(const maskChanged = state.contacts_person_fields !== CONTACT_PERSON_FIELDS;// #8906: a full re-list that a deadline or the page budget stopped resumes at its checkpointed page. --full lists// from page 1 to the end in one run, as it did before the checkpoint.- const resumeToken = !deps.opts.full && state.contacts_relist?.person_fields === CONTACT_PERSON_FIELDS ? state.contacts_relist.page_token : null;+ const relist = !deps.opts.full && state.contacts_relist?.person_fields === CONTACT_PERSON_FIELDS ? state.contacts_relist : null;+ const resumeToken = relist?.page_token ?? null;const pageBudget = deps.opts.full ? Infinity : CONTACT_RELIST_PAGES_PER_RUN;if (deps.opts.full) state.contacts_relist = null;const syncToken = deps.opts.full || maskChanged || resumeToken ? null : state.contacts_sync_token;if (maskChanged && state.contacts_sync_token && !deps.opts.full && !resumeToken) deps.log('[google] contacts field mask changed; full re-list');let groupNames: Record<string, string> | null = null;const sweep = () => ({ activePack, summary, countedSlugs, groupNames: groupNames ?? state.contact_groups ?? {} });- let blocked = false;+ // A re-list that passed a failing contact not yet held stays blocked until it ends, so its sync token is never saved.+ let blocked = relist?.blocked === true;// One listed batch through the holds; false when the run was aborted part-way.const apply = async (contacts: ContactData[]): Promise<boolean> => {groupNames ??= contacts.some((c) => !c.deleted && c.groups.length > 0) ? await resolveContactGroups(deps, people, state) : null;@@ -98,9 +100,7 @@ export async function sweepContacts(// list from page 1, once.let pageToken = resumeToken;if (resumeToken) deps.log('[google] contacts full re-list resuming at its checkpointed page');- // A failing contact not yet held freezes the cursor before its page, so while blocked the run lists to the end, as- // it did before the budget: every later contact still lands in this run.- for (let pages = 0; (pages < pageBudget || blocked) && !listed; pages++) {+ for (let pages = 0; pages < pageBudget && !listed; pages++) {let page;try {page = await people.listConnectionsPage(pageToken, signal);@@ -108,6 +108,7 @@ export async function sweepContacts(if (pages > 0 || !resumeToken || !(e instanceof GoogleCursorExpiredError)) throw e;deps.log('[google] contacts re-list page token expired; restarting the full re-list from page 1');state.contacts_relist = null;+ blocked = false;page = await people.listConnectionsPage(pageToken = null, signal);}if (!await apply(page.contacts)) return;@@ -117,9 +118,9 @@ export async function sweepContacts(continue;}pageToken = page.nextPageToken;- // A failing contact not yet held keeps the cursor before its page; a managed lease cannot write once aborted.- if (blocked || deps.opts.signal?.aborted) continue;- state.contacts_relist = { page_token: pageToken, person_fields: CONTACT_PERSON_FIELDS };+ // A managed lease cannot write once aborted.+ if (deps.opts.signal?.aborted) continue;+ state.contacts_relist = { page_token: pageToken, person_fields: CONTACT_PERSON_FIELDS, ...(blocked ? { blocked: true } : {}) };await checkpoint();}if (!listed) deps.log(`[google] contacts full re-list paused after ${CONTACT_RELIST_PAGES_PER_RUN} pages; the next sync resumes it`);@@ -134,6 +135,8 @@ export async function sweepContacts(await applyContact(deps, c, sweep());});}+ // A blocked re-list that reached its last page starts over at page 1 next sync, retrying the contact that failed.+ if (blocked && listed) state.contacts_relist = null;if (blocked || !listed) return;// Cursor and the mask it was minted under commit together, only after the// whole sweep succeeded. A full list that returned no replacement tokendiff --git a/src/core/google/types.ts b/src/core/google/types.tsindex 93637acac0..dedaddf800 100644--- a/src/core/google/types.ts+++ b/src/core/google/types.ts@@ -189,7 +189,7 @@ export interface GoogleSourceState {* each page lands so a run stopped by its deadline or page budget resumes* there. Cleared when the list completes; one under another mask is ignored.*/- contacts_relist?: { page_token: string; person_fields: string } | null;+ contacts_relist?: { page_token: string; person_fields: string; blocked?: boolean } | null;/*** #6324: contact-group directory cache (resource name → display name) from* the last successful `contactGroups` read; the fallback when the directorydiff --git a/src/core/persistence/source-lifecycle.ts b/src/core/persistence/source-lifecycle.tsindex 4780a51357..f1060c3793 100644--- a/src/core/persistence/source-lifecycle.ts+++ b/src/core/persistence/source-lifecycle.ts@@ -185,12 +185,14 @@ export async function runManagedSourceLifecycle(engine:BrainEngine,input:SourceLmanifests.set(root.worktree,worktreeManifest(root.worktree,{progress:humanManifestProgress(),scope:storedManifestScope(manifests.get(currentRoot))}));// #7702/#8905: a remove or purge first deletes the source's pages in bounded batches, each its own transaction behind// the final transaction's preconditions; any precondition that would refuse or replay stops batching and the final- // transaction reports it. Committed batches are durable, so a retry with the same request_id resumes. A batch that- // hits the statement bound is retried at half the size, so heavy pages still fit; a lock wait or a busy writer stops- // the run with write_pending instead, and the retry resumes at full size.+ // transaction reports it. Committed batches are durable, so a retry with the same request_id resumes. A batch+ // whose DELETE hits the statement bound is retried at half the size, so heavy pages still fit; any other bounded wait+ // (a lock wait, a busy writer, a slow precondition or commit) stops the run with write_pending, and the retry resumes at+ // full size. Only committed batches count toward the pages the run reports.let retiredPages=0;try{- const retireBatch=async(limit:number):Promise<number|null>=>{ try{ return await topologyTransaction(engine,async tx=>{+ let deleting=false;+ const retireBatch=async(limit:number):Promise<number|null>=>{ deleting=false; try{ const n=await topologyTransaction(engine,async tx=>{await assertWriterAdminState(tx,input.expectedAdminState);const sources=await lockTopologyRows(tx,input.sourceId,bindings);const [source]=await tx.executeRaw<{incarnation:string;archived:boolean;due:boolean}>(@@ -201,13 +203,15 @@ export async function runManagedSourceLifecycle(engine:BrainEngine,input:SourceLawait assertTopologyRequestsSettled(tx,sources,bindings.map(binding=>binding.worktree_id));await lockTopologyPrincipal(tx,principal);await assertTopologyChangeCapacity(tx,{principal,operation:input.operation,sourceId:input.sourceId});- const gone=await withCoordinatedWrite(tx,sources,()=>tx.executeRaw(- 'DELETE FROM pages WHERE id IN (SELECT id FROM pages WHERE source_id=$1 LIMIT $2) RETURNING id',[input.sourceId,limit]),- principalAttribution({kind:'local_cli',id:principal}));- retiredPages+=gone.length;+ const gone=await withCoordinatedWrite(tx,sources,async()=>{+ deleting=true;+ const rows=await tx.executeRaw('DELETE FROM pages WHERE id IN (SELECT id FROM pages WHERE source_id=$1 LIMIT $2) RETURNING id',[input.sourceId,limit]);+ deleting=false;+ return rows;+ },principalAttribution({kind:'local_cli',id:principal}));return gone.length;- }); }catch(error){- if(error instanceof OperationError&&error.code==='write_pending'&&error.reason==='statement_timeout'&&limit>1) return null;+ }); retiredPages+=n; return n; }catch(error){+ if(deleting&&error instanceof OperationError&&error.code==='write_pending'&&error.reason==='statement_timeout'&&limit>1) return null;throw error;} };if(['remove','purge'].includes(input.operation)&&input.confirmDestructive&&!missingCheckout&&!admission)diff --git a/test/contacts-relist-checkpoint-8906.slow.test.ts b/test/contacts-relist-checkpoint-8906.slow.test.tsindex ad891ac242..44a8fb0925 100644--- a/test/contacts-relist-checkpoint-8906.slow.test.ts+++ b/test/contacts-relist-checkpoint-8906.slow.test.ts@@ -166,6 +166,19 @@ describe('contacts full re-list checkpoints (#8906)', () => {}}, 120_000);+ test('a paginated HTTP 400 that does not name the page token propagates: no restart from page 1', async () => {+ const { dir, cfg } = await googleSource();+ try {+ seedState(dir, { contacts_sync_token: null, contacts_person_fields: CONTACT_PERSON_FIELDS, contacts_relist: { page_token: 'gone-page', person_fields: CONTACT_PERSON_FIELDS } });+ const api = peopleApi(3, { stale: () => json({ error: { code: 400, message: 'Invalid personFields mask path: "unknownField".' } }, 400) });+ expect((await sync(cfg, api.fetchImpl)).status).toBe('partial');+ expect(api.requests.map((r) => r.pageToken)).toEqual(['gone-page']);+ expect(relistOf(dir)?.page_token).toBe('gone-page');+ } finally {+ rmSync(dir, { recursive: true, force: true });+ }+ }, 60_000);+test('a page token saved under another field mask is not resumed: the re-list starts at page 1', async () => {const { dir, cfg } = await googleSource();try {@@ -197,18 +210,26 @@ describe('contacts full re-list checkpoints (#8906)', () => {rmSync(dir, { recursive: true, force: true });}}, 240_000);-test('a contact that keeps failing transiently does not hide the contacts past the page budget: a blocked run lists to the end', async () => {+test('a contact that keeps failing transiently keeps the page budget: the cursor moves past it, the finished list saves no sync token, and the next pass retries it', async () => {const { dir, cfg } = await googleSource();await engine.executeRaw(`CREATE FUNCTION relist_fail_fn() RETURNS trigger LANGUAGE plpgsql AS $$ BEGINIF NEW.title = 'Person c00200 Example' THEN RAISE EXCEPTION 'injected transient contact failure'; END IF; RETURN NEW; END $$`);await engine.executeRaw('CREATE TRIGGER relist_fail BEFORE INSERT ON pages FOR EACH ROW EXECUTE FUNCTION relist_fail_fn()');try {- const api = peopleApi(12);+ const api = peopleApi(11);expect((await sync(cfg, api.fetchImpl)).status).toBe('partial');- expect(api.served).toEqual(Array.from({ length: 12 }, (_, i) => 'p' + (i + 1)));- expect(await personPages()).toBe(2_399);- expect(relistOf(dir)?.page_token).toBe('p2');+ expect(api.served).toEqual(Array.from({ length: 10 }, (_, i) => 'p' + (i + 1)));+ expect(relistOf(dir)).toEqual({ page_token: 'p11', person_fields: CONTACT_PERSON_FIELDS, blocked: true });+ await sync(cfg, api.fetchImpl);+ expect(api.served.slice(10)).toEqual(['p11']);+ expect(await personPages()).toBe(2_199);expect(readGoogleState(dir).contacts_sync_token).toBeNull();+ expect(relistOf(dir)).toBeNull();+ await engine.executeRaw('DROP TRIGGER relist_fail ON pages');+ await sync(cfg, api.fetchImpl);+ expect(api.requests.at(-10)).toEqual({ syncToken: null, pageToken: null });+ expect(await personPages()).toBe(2_200);+ expect(relistOf(dir)).toEqual({ page_token: 'p11', person_fields: CONTACT_PERSON_FIELDS });} finally {for (const sql of ['DROP TRIGGER IF EXISTS relist_fail ON pages', 'DROP FUNCTION IF EXISTS relist_fail_fn()']) await engine.executeRaw(sql);rmSync(dir, { recursive: true, force: true });diff --git a/test/persistence-connectors.test.ts b/test/persistence-connectors.test.tsindex fa18d15ee6..db863130ef 100644--- a/test/persistence-connectors.test.ts+++ b/test/persistence-connectors.test.ts@@ -96,11 +96,12 @@ test('Google pagination failure keeps the landed page and resumes at the failedexpect((await runGoogleSync(engine, f.id, cfg, options, withGoogleAccount(fetcher))).status).toBe('partial');// #8906: the page that landed stays, and the re-list resumes at the page that failed.expect(await engine.executeRaw('SELECT slug FROM pages WHERE source_id=$1', [f.id])).toEqual([{ slug: 'people/first-example' }]);+ expect(cursorOf(await sourceCheckpoint(engine, f.id)).flat().map(({ state }) => state?.contacts_sync_token ?? null)).toEqual([null]);fail = false;listed.length = 0;expect((await runGoogleSync(engine, f.id, cfg, options, withGoogleAccount(fetcher))).added).toBe(1);expect(listed).toEqual(['second']);- expect((await engine.executeRaw('SELECT slug FROM pages WHERE source_id=$1 ORDER BY slug', [f.id])).map((r: { slug: string }) => r.slug)).toEqual(['people/first-example', 'people/second-example']);+ expect((await engine.executeRaw<{ slug: string }>('SELECT slug FROM pages WHERE source_id=$1 ORDER BY slug', [f.id])).map((r) => r.slug)).toEqual(['people/first-example', 'people/second-example']);const other = await source(engine, googleConfig);expect((await runGoogleSync(engine, other.id, parseGoogleSourceConfig(googleConfig, other.dir), options, withGoogleAccount(fetcher))).added).toBe(2);deleted = true;diff --git a/test/source-retire-batched.test.ts b/test/source-retire-batched.test.tsindex 328b7968c9..845fbd98ac 100644--- a/test/source-retire-batched.test.ts+++ b/test/source-retire-batched.test.ts@@ -101,6 +101,27 @@ test('a lock wait stops the remove with write_pending instead of shrinking the b}}), 60_000);+test('a bounded wait at COMMIT stops the remove without halving, and only committed batches count', () => fixture(async () => {+ await engine.executeRaw('CREATE SEQUENCE retire_commit_seq');+ await engine.executeRaw(`CREATE FUNCTION retire_commit_fn() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN+ IF nextval('retire_commit_seq') = ${SOURCE_RETIRE_BATCH_PAGES + 1} THEN RAISE EXCEPTION 'canceling statement due to statement timeout' USING ERRCODE = TG_ARGV[0]; END IF;+ RETURN NULL; END $$`);+ try {+ for (const errcode of ['query_canceled', 'lock_not_available']) {+ await engine.executeRaw('SELECT setval($1, 1, false)', ['retire_commit_seq']);+ await engine.executeRaw(`CREATE CONSTRAINT TRIGGER retire_commit AFTER DELETE ON pages DEFERRABLE INITIALLY DEFERRED FOR EACH ROW EXECUTE FUNCTION retire_commit_fn('${errcode}')`);+ const before = await pages(source);+ const err = await runManagedSourceLifecycle(engine, { operation: 'remove', sourceId: source, confirmDestructive: true }).then(() => null, (e: Error & { code?: string; suggestion?: string }) => e);+ await engine.executeRaw('DROP TRIGGER retire_commit ON pages');+ expect({ errcode, code: err?.code }).toEqual({ errcode, code: 'write_pending' });+ expect(await pages(source)).toBe(before - SOURCE_RETIRE_BATCH_PAGES);+ expect(err?.suggestion).toContain(`already deleted ${SOURCE_RETIRE_BATCH_PAGES} page(s)`);+ }+ } finally {+ for (const sql of ['DROP TRIGGER IF EXISTS retire_commit ON pages', 'DROP FUNCTION IF EXISTS retire_commit_fn()', 'DROP SEQUENCE IF EXISTS retire_commit_seq']) await engine.executeRaw(sql);+ }+}), 60_000);+test('an interrupted batch keeps the committed batches and the same request_id resumes to completion', () => fixture(async () => {await engine.executeRaw('CREATE TABLE retire_fail(armed boolean NOT NULL, seen integer NOT NULL)');await engine.executeRaw(`CREATE FUNCTION retire_fail_fn() RETURNS trigger LANGUAGE plpgsql AS $$ DECLARE n integer; BEGIN