Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix: pre-landing review fixes (Fireflies not_authorized is access denied; tests for connector client guards, rate-limit dispatch hold, database-cursor hold carry)
garrytan7f91f7ad3c00
diff --git a/src/core/meetings/providers/fireflies.ts b/src/core/meetings/providers/fireflies.tsindex c89985a2bd..5375ec0da2 100644--- a/src/core/meetings/providers/fireflies.ts+++ b/src/core/meetings/providers/fireflies.ts@@ -44,10 +44,10 @@ export function classifyFirefliesBody(body: unknown, now: number): void {const code = lower(first.extensions?.code ?? first.code);const message = lower(first.message);const text = typeof first.message === 'string' ? first.message.slice(0, 200) : 'unknown error';+ if (/forbidden|permission|not_authorized|privilege/.test(code)) throw new ConnectorAccessDeniedError(`fireflies: ${text}`);if (/auth|unauthenticated|invalid_api_key|api key/.test(code) || /invalid api key|unauthori[sz]ed|authentication/.test(message)) {throw new ConnectorAuthError(`fireflies: the API key was rejected (${code || 'auth'})`);}- if (/forbidden|permission|not_authorized|privilege/.test(code)) throw new ConnectorAccessDeniedError(`fireflies: ${text}`);if (/too_many|rate|quota|limit/.test(code) || /rate limit|too many requests|quota/.test(message)) {const retryAfter = first.extensions?.metadata?.retryAfter;const retryAt = typeof retryAfter === 'number' && retryAfter > 0diff --git a/test/autopilot-connector-dispatch.test.ts b/test/autopilot-connector-dispatch.test.tsindex 7a90bd153b..525efcbfbf 100644--- a/test/autopilot-connector-dispatch.test.ts+++ b/test/autopilot-connector-dispatch.test.ts@@ -20,6 +20,7 @@ import { withConnectorSync } from '../src/core/persistence/connector-sync.ts';import { attemptedConnectorSourceIds, recordConnectorSyncAttempt, seedConnectorDispatchAttempts } from '../src/core/persistence/connector-state.ts';import { CONNECTOR_SOURCE_PHASES } from '../src/core/cycle/phase-scope.ts';import { LATEST_VERSION } from '../src/core/migrate.ts';+import { recordRateLimit } from '../src/core/connectors/api-source.ts';let engine: PGLiteEngine;let dir: string;@@ -146,3 +147,16 @@ describe('#5673 connector dispatch gate', () => {});afterAll(() => { rmSync(dir, { recursive: true, force: true }); });++describe('connector provider rate limits', () => {+ test('a recorded provider reset holds the connector sync until it passes', async () => {+ await addSource('slack-a', { kind: 'slack', s_channels: 'CGENERAL01' });+ await recordConnectorSyncAttempt(engine, 'slack-a');+ await recordRateLimit(engine, 'slack-a', Date.now() + 600_000);+ await freshness([]);+ expect(await jobs()).toEqual([]);+ await recordRateLimit(engine, 'slack-a', Date.now() - 1_000);+ await freshness([]);+ expect((await jobs()).map(job => [job.name, job.data.sourceId])).toEqual([['sync', 'slack-a']]);+ });+});diff --git a/test/connector-client-guards.test.ts b/test/connector-client-guards.test.tsnew file mode 100644index 0000000000..e4b8af3c74--- /dev/null+++ b/test/connector-client-guards.test.ts@@ -0,0 +1,40 @@+/**+ * ConnectorClient guards the API-token sources (meetings, slack) rely on: a body past maxResponseBytes fails+ * connector_response_too_large instead of being buffered, a stalled request fails as a network TIMEOUT, and a+ * caller's abort surfaces as its own reason rather than a network error. Injected fetch only; no network.+ */+import { describe, expect, test } from 'bun:test';+import { ConnectorClient, ConnectorNetworkError, ConnectorResponseTooLargeError, type ConnectorFetch } from '../src/core/connectors/client.ts';++const client = (fetchImpl: ConnectorFetch, extra: { maxResponseBytes?: number; requestTimeoutMs?: number } = {}) =>+ new ConnectorClient({ baseUrl: 'https://api.example.invalid', headers: async () => ({}), minDelayMs: 0, fetchImpl, ...extra });+const stalled: ConnectorFetch = (_url, init) => new Promise((_resolve, reject) => {+ if (init?.signal?.aborted) reject(init.signal.reason);+ init?.signal?.addEventListener('abort', () => reject(init.signal!.reason));+});++describe('ConnectorClient guards', () => {+ test('a body over maxResponseBytes fails and is not read whole', async () => {+ let pulled = 0;+ const body = new ReadableStream<Uint8Array>({ pull(controller) { pulled++; controller.enqueue(new Uint8Array(64)); } });+ const c = client(async () => new Response(body, { status: 200 }), { maxResponseBytes: 100 });+ await expect(c.fetchJSON('/x')).rejects.toBeInstanceOf(ConnectorResponseTooLargeError);+ expect(pulled).toBeLessThan(5);+ const small = client(async () => new Response('{"ok":true}', { status: 200 }), { maxResponseBytes: 100 });+ expect(await small.postJSON('/x', { a: 1 })).toEqual({ ok: true });+ });++ test('a stalled request fails as a network timeout', async () => {+ const error = await client(stalled, { requestTimeoutMs: 20 }).fetchJSON('/x').catch(e => e);+ expect(error).toBeInstanceOf(ConnectorNetworkError);+ expect(String(error.message)).toContain('TIMEOUT');+ });++ test("a caller's abort surfaces as its reason, not a network error", async () => {+ const controller = new AbortController();+ const reason = new Error('stopped by the caller');+ const pending = client(stalled, { requestTimeoutMs: 60_000 }).fetchJSON('/x', { signal: controller.signal });+ controller.abort(reason);+ expect(await pending.catch(e => e)).toBe(reason);+ });+});diff --git a/test/hold-carry-classic-state.test.ts b/test/hold-carry-classic-state.test.tsindex 9bf404100e..db0c37a011 100644--- a/test/hold-carry-classic-state.test.ts+++ b/test/hold-carry-classic-state.test.ts@@ -18,6 +18,7 @@ import { PGLiteEngine } from '../src/core/pglite-engine.ts';import { carryHoldsToClassicState, holdCarryBlocked, planHoldCarry } from '../src/core/connectors/item-holds-store.ts';import { carryLegacyFailCounts } from '../src/core/connectors/item-holds.ts';import { connectorCheckpointKey, connectorIdentity } from '../src/core/persistence/connector-identity.ts';+import { connectorDbCursorKey, readConnectorDbCursor, writeConnectorDbCursor } from '../src/core/connectors/db-cursor.ts';let engine: PGLiteEngine;let dir: string;@@ -88,3 +89,26 @@ describe('carrying connector holds into classic state', () => {expect(holdCarryBlocked((await planHoldCarry(engine, other))[0])).toBe(false);});});++describe('carrying holds of a database-recorded connector source', () => {+ test('a slack source with no checkout takes its managed holds into its database cursor, other state kept', async () => {+ const slack = { kind: 'slack', s_channels: 'CGENERAL01', s_record: 'database' };+ await engine.executeRaw('INSERT INTO sources (id, name, local_path, config) VALUES ($1, $1, NULL, $2::text::jsonb)', ['slack-a', JSON.stringify(slack)]);+ try {+ const [row] = await engine.executeRaw<{ incarnation: string }>("SELECT incarnation::text AS incarnation FROM sources WHERE id='slack-a'");+ const identity = connectorIdentity('slack', slack, null);+ await engine.executeRaw(`INSERT INTO op_checkpoints(op,fingerprint,completed_keys) VALUES('managed-connector',$1,$2::text::jsonb)`,+ [connectorCheckpointKey('slack-a', row.incarnation, identity), JSON.stringify([{ state: { channels: {}, item_holds: held() } }])]);+ const cursor = connectorDbCursorKey('slack-a', row.incarnation, identity);+ await writeConnectorDbCursor(engine, cursor, { channels: { CGENERAL01: { committed_to: '2026-10-01T00:00:00.000Z' } } });+ const carried = await carryHoldsToClassicState(engine, HOST);+ expect(carried.filter(c => c.source_id === 'slack-a').map(c => c.items)).toEqual([1]);+ const state = (await readConnectorDbCursor(engine, cursor)).state as Record<string, unknown>;+ expect(state.channels).toEqual({ CGENERAL01: { committed_to: '2026-10-01T00:00:00.000Z' } });+ expect(state.item_holds).toEqual(JSON.parse(JSON.stringify(held())));+ } finally {+ await engine.executeRaw("DELETE FROM op_checkpoints WHERE fingerprint LIKE '%slack-a%'");+ await engine.executeRaw("DELETE FROM sources WHERE id='slack-a'");+ }+ });+});diff --git a/test/meetings-unit.test.ts b/test/meetings-unit.test.tsindex 86b37838c8..d4e3716b4e 100644--- a/test/meetings-unit.test.ts+++ b/test/meetings-unit.test.ts@@ -31,6 +31,7 @@ describe('Fireflies responses', () => {const err = (code: string, message = 'x', extra: Record<string, unknown> = {}) => ({ errors: [{ message, extensions: { code, ...extra } }] });expect(() => classifyFirefliesBody(err('auth_failed', 'Invalid API key'), NOW)).toThrow(ConnectorAuthError);expect(() => classifyFirefliesBody(err('forbidden'), NOW)).toThrow(ConnectorAccessDeniedError);+ expect(() => classifyFirefliesBody(err('not_authorized', 'Not authorized to read this team'), NOW)).toThrow(ConnectorAccessDeniedError);expect(() => classifyFirefliesBody(err('bad_request'), NOW)).toThrow(MeetingProviderError);expect(() => classifyFirefliesBody(err('object_not_found'), NOW)).not.toThrow();expect(() => classifyFirefliesBody({ data: {} }, NOW)).not.toThrow();