Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions src/issues/linearBridge.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
// ============================================
// OpenSwarm - Linear bridge inbound sync pagination tests (AGT-3421)
// ============================================

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { SqliteIssueStore } from './sqliteStore.js';
import { drainLinearIssuesToStore, LINEAR_SYNC_MAX_PAGES, type LinearIssuePage } from './linearBridge.js';

function linearNode(id: string, priority = 3): Record<string, unknown> {
return {
id,
identifier: `INT-${id}`,
title: `Linear issue ${id}`,
url: `https://linear.app/i/${id}`,
description: 'desc',
priority,
state: Promise.resolve({ name: 'Todo' }),
};
}

function page(nodes: unknown[], opts?: { hasNextPage?: boolean; endCursor?: string }): LinearIssuePage {
return {
nodes: nodes as any[],
hasNextPage: opts?.hasNextPage ?? false,
endCursor: opts?.endCursor,
};
}

describe('drainLinearIssuesToStore', () => {
let store: SqliteIssueStore;
let consoleError: ReturnType<typeof vi.spyOn>;
let consoleWarn: ReturnType<typeof vi.spyOn>;

beforeEach(() => {
store = new SqliteIssueStore(':memory:');
consoleError = vi.spyOn(console, 'error').mockImplementation(() => {});
consoleWarn = vi.spyOn(console, 'warn').mockImplementation(() => {});
});

afterEach(() => {
consoleError.mockRestore();
consoleWarn.mockRestore();
store.close();
});

it('applies the limit to the TOTAL imported count across pages, not per page', async () => {
// Two pages of 30 with a total limit of 50: 50 must be imported and the
// run must stop there (the old code capped each page at `limit`).
const pages = [
page(Array.from({ length: 30 }, (_, i) => linearNode(`a${i}`)), { hasNextPage: true, endCursor: 'c1' }),
page(Array.from({ length: 30 }, (_, i) => linearNode(`b${i}`)), { hasNextPage: false }),
];
let calls = 0;
const fetchPage = async () => pages[calls++] ?? page([]);

const progress = await drainLinearIssuesToStore(store, 'proj', fetchPage, 50);

expect(progress.created).toBe(50);
expect(progress.updated).toBe(0);
expect(progress.truncated).toBe(true);
expect(progress.failed).toBeUndefined();
expect(calls).toBe(2); // never fetched a third page
// Durable: the 50th imported issue is queryable by its linear id.
expect(store.getIssueByLinearId('b19')).not.toBeNull();
expect(store.listIssues({ source: 'linear', limit: 500, offset: 0 }).total).toBe(50);
});

it('bails with an explicit error on a cursor that never advances (repeated)', async () => {
const fetchPage = async (after?: string) =>
page(Array.from({ length: 3 }, (_, i) => linearNode(`n${after ?? '0'}-${i}`)), {
hasNextPage: true,
endCursor: 'stuck',
});

const progress = await drainLinearIssuesToStore(store, 'proj', fetchPage, 1000);

// Terminated BEFORE applying the re-served window: only page one is durable.
expect(progress.failed?.message).toMatch(/repeated cursor/);
expect(progress.created).toBe(3);
expect(store.listIssues({ source: 'linear', limit: 500, offset: 0 }).total).toBe(3);
expect(consoleError).toHaveBeenCalledWith(
expect.stringContaining('created 3, updated 0'),
expect.any(Error),
);
});

it('bails with an explicit error on a missing cursor', async () => {
const fetchPage = async () =>
page([linearNode('only')], { hasNextPage: true, endCursor: undefined });

const progress = await drainLinearIssuesToStore(store, 'proj', fetchPage, 1000);

// The first page already claims more data but offers no cursor to continue
// with — refuse to apply it rather than import an unverifiable window.
expect(progress.failed?.message).toMatch(/missing cursor/);
expect(progress.created).toBe(0);
});

it('reports the durable import count when an issue fails mid-run', async () => {
// First issue creates fine; the second (an existing local issue) fails to
// update. Everything before the failure is committed and reported.
store.createIssue({ projectId: 'proj', title: 'existing', source: 'linear', linearId: 'lin-2' });
vi.spyOn(store, 'updateIssue').mockImplementation(() => {
throw new Error('update exploded');
});

const fetchPage = async () =>
page([linearNode('lin-1'), linearNode('lin-2'), linearNode('lin-3')], { hasNextPage: false });

const progress = await drainLinearIssuesToStore(store, 'proj', fetchPage, 1000);

expect(progress.created).toBe(1);
expect(progress.updated).toBe(0);
expect(progress.failed).toBeInstanceOf(Error);
expect(progress.failed?.message).toBe('update exploded');
expect(store.getIssueByLinearId('lin-1')).not.toBeNull(); // durable
expect(store.getIssueByLinearId('lin-3')).toBeNull(); // never reached
expect(consoleError).toHaveBeenCalledWith(
expect.stringContaining('created 1, updated 0'),
expect.anything(),
);
});

it('stops deterministically at the page cap even with an endless connection', async () => {
let cursor = 0;
const fetchPage = async () => page([linearNode(`p${cursor}`)], {
hasNextPage: true,
endCursor: `c${++cursor}`,
});

const progress = await drainLinearIssuesToStore(store, 'proj', fetchPage, Number.MAX_SAFE_INTEGER);

expect(cursor).toBe(LINEAR_SYNC_MAX_PAGES);
expect(progress.created).toBe(LINEAR_SYNC_MAX_PAGES);
expect(progress.truncated).toBe(true);
expect(consoleWarn).toHaveBeenCalledWith(expect.stringContaining('페이지 상한'));
});

it('reports a clean end when the connection terminates on its own', async () => {
const fetchPage = async () => page([linearNode('a')], { hasNextPage: false });

const progress = await drainLinearIssuesToStore(store, 'proj', fetchPage, 50);

expect(progress).toEqual({ created: 1, updated: 0, truncated: false });
});
});
169 changes: 132 additions & 37 deletions src/issues/linearBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,111 @@ export function initLinearBridge(apiKey: string, teamId: string): Promise<void>
return linearInitPromise;
}

/**
* Page size for the inbound sync cursor loop. Linear reliably serves pages of
* 50; the sync limit is applied to the TOTAL across pages, not per page.
*/
const LINEAR_SYNC_PAGE_SIZE = 50;
/**
* Hard page cap for one sync run (50 × 20 = 1000 issues). Bounds worst-case
* API work when the workspace has far more matching issues than the limit:
* the run stops deterministically and reports what it committed.
*/
export const LINEAR_SYNC_MAX_PAGES = 20;

/** One page of the Linear issue connection, adapted from the SDK response. */
export interface LinearIssuePage {
nodes: any[];
hasNextPage: boolean;
endCursor?: string;
}

/** Durable outcome of an inbound sync run. */
export interface LinearSyncProgress {
created: number;
updated: number;
/** True when the run stopped at the total limit or page cap with data left. */
truncated: boolean;
/** The error that aborted the run; everything before it is already committed. */
failed?: Error;
}

/**
* Drain a Linear issue connection page by page, applying each issue to the
* local store. `maxItems` caps the TOTAL imported count across pages (never
* per page). Termination is deterministic: total limit, page cap, or an
* exhausted connection. A missing/repeated cursor aborts the run with an
* explicit error instead of looping forever.
*
* Never throws: a per-issue failure (or cursor anomaly) is returned as
* `failed` alongside the counts that were durably committed before it.
*/
export async function drainLinearIssuesToStore(
store: SqliteIssueStore,
projectId: string,
fetchPage: (after: string | undefined) => Promise<LinearIssuePage>,
maxItems: number,
): Promise<LinearSyncProgress> {
let created = 0;
let updated = 0;
let after: string | undefined;

try {
for (let page = 0; page < LINEAR_SYNC_MAX_PAGES; page++) {
const conn = await fetchPage(after);
// Cursor sanity BEFORE applying: a response whose endCursor is absent or
// identical to the cursor we just used re-served the same window — bail
// instead of double-applying its nodes or looping forever. (AGT-3421)
if (conn.hasNextPage === true && (!conn.endCursor || conn.endCursor === after)) {
throw new Error(
`Linear inbound sync pagination returned a ${conn.endCursor ? 'repeated' : 'missing'} cursor on page ${page + 1}`,
);
}
for (const issue of conn.nodes) {
// The limit is global: stop as soon as the total across all pages is met.
if (created + updated >= maxItems) {
return { created, updated, truncated: true };
}
const existing = findByLinearId(store, issue.id);
const linearData = await mapLinearToLocal(issue, projectId);

if (existing) {
// 이미 존재 → 업데이트
store.updateIssue(existing.id, linearData);
updated++;
} else {
// 새 이슈 → 생성
store.createIssue({
...linearData,
source: 'linear',
linearId: issue.id,
linearIdentifier: issue.identifier,
linearUrl: issue.url,
});
created++;
}
}

if (conn.hasNextPage !== true) {
return { created, updated, truncated: false };
}
after = conn.endCursor;
}

// Page cap exhausted while the connection still reported more pages.
console.warn(`[LinearBridge] 페이지 상한 ${LINEAR_SYNC_MAX_PAGES}개 도달 — 이번 동기화는 여기서 멈춥니다.`);
return { created, updated, truncated: true };
} catch (err) {
const failed = err instanceof Error ? err : new Error(String(err));
// Partial failure: everything applied so far is durable — say so.
console.error(
`[LinearBridge] 동기화 중단 — 지금까지 커밋됨: created ${created}, updated ${updated}`,
failed,
);
return { created, updated, truncated: false, failed };
}
}

/**
* Linear → 로컬: Linear 이슈를 로컬 DB에 동기화
*/
Expand All @@ -178,46 +283,36 @@ export async function syncFromLinear(
const states = options?.states ?? ['In Progress', 'Todo', 'Backlog', 'In Review', 'Done', 'Canceled', 'Cancelled'];
const limit = options?.limit ?? 50;

let created = 0;
let updated = 0;

try {
const issues = await linearClient.issues({
filter: {
team: { id: { eq: linearTeamId } },
state: { name: { in: states } },
},
first: limit,
orderBy: 'updatedAt',
});

for (const issue of issues.nodes) {
const existing = findByLinearId(store, issue.id);
const linearData = await mapLinearToLocal(issue, projectId);

if (existing) {
// 이미 존재 → 업데이트
store.updateIssue(existing.id, linearData);
updated++;
} else {
// 새 이슈 → 생성
store.createIssue({
...linearData,
source: 'linear',
linearId: issue.id,
linearIdentifier: issue.identifier,
linearUrl: issue.url,
});
created++;
}
}
const progress = await drainLinearIssuesToStore(
store,
projectId,
async (after) => {
const conn = await linearClient.issues({
filter: {
team: { id: { eq: linearTeamId } },
state: { name: { in: states } },
},
first: LINEAR_SYNC_PAGE_SIZE,
after,
orderBy: 'updatedAt',
});
return {
nodes: conn.nodes,
hasNextPage: conn.pageInfo?.hasNextPage === true,
endCursor: conn.pageInfo?.endCursor,
};
},
limit,
);

console.log(`[LinearBridge] 동기화 완료 — created: ${created}, updated: ${updated}`);
} catch (err) {
console.error('[LinearBridge] 동기화 실패:', err);
if (progress.failed) {
console.error('[LinearBridge] 동기화 실패:', progress.failed);
} else {
const truncatedNote = progress.truncated ? ` (limit ${limit} 도달)` : '';
console.log(`[LinearBridge] 동기화 완료 — created: ${progress.created}, updated: ${progress.updated}${truncatedNote}`);
}

return { created, updated };
return { created: progress.created, updated: progress.updated };
}

/**
Expand Down
Loading
Loading