Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(desktop): support worktree references in inline db worker

DavidShen340d45cbddf3
diff --git a/apps/desktop/src/main/localDb/client/WorkerThreadTransport.ts b/apps/desktop/src/main/localDb/client/WorkerThreadTransport.ts
index 5e59311637..673d6cecfe 100644
--- a/apps/desktop/src/main/localDb/client/WorkerThreadTransport.ts
+++ b/apps/desktop/src/main/localDb/client/WorkerThreadTransport.ts
@@ -348,6 +348,58 @@ function requireReadyDb() {
throw err;
}
+function worktreeReferenceQuery(nextDb) {
+ const info = nextDb.prepare('PRAGMA table_info(sessions)').all();
+ const columns = new Set(info.map((column) => column.name));
+ const hasWorktree = columns.has('worktree_path');
+ const hasSource = columns.has('source');
+ const hasRemote = columns.has('remote_host_id');
+ if (!['id', 'status', 'working_dir'].every((name) => columns.has(name))
+ || (hasSource && !hasWorktree) || (hasRemote && !hasSource)) {
+ throw new Error('unsupported task reference schema');
+ }
+ const status = hasRemote ? 'status' : 'NULL';
+ return 'SELECT id, ' + status + ' AS status, ' + (hasSource ? 'source' : 'NULL') + ' AS source, '
+ + 'working_dir AS workingDir, ' + (hasWorktree ? 'worktree_path' : 'NULL') + ' AS worktreePath '
+ + 'FROM sessions' + (hasRemote ? ' WHERE remote_host_id IS NULL' : '');
+}
+
+function readLocalWorktreeReferences() {
+ const location = db.prepare('PRAGMA database_list').all();
+ const main = location.find((entry) => entry.name === 'main');
+ const databasePath = main && main.file;
+ if (!databasePath || !path.isAbsolute(databasePath)) throw new Error('task database path unavailable');
+ const currentPath = path.resolve(databasePath);
+ const root = path.dirname(currentPath);
+ const profiles = path.join(root, 'profiles');
+ if (fs.existsSync(profiles) && fs.readdirSync(profiles).length) {
+ throw new Error('profile database catalog requires a compatible reader');
+ }
+ const names = fs.readdirSync(root).filter((name) => /^(?:cindy|xdt)-.+\.db$/.test(name));
+ if (!names.includes(path.basename(currentPath))) throw new Error('unknown task database layout');
+ const rows = [];
+ for (const name of names) {
+ const file = path.join(root, name);
+ if (!fs.lstatSync(file).isFile()) throw new Error('task database is not a regular file');
+ const isCurrent = file === currentPath;
+ const nextDb = isCurrent ? db : new Database(file, {
+ readonly: true, fileMustExist: true,
+ ...(workerData && workerData.nativeBinding ? { nativeBinding: workerData.nativeBinding } : {}),
+ });
+ try {
+ const references = nextDb.transaction(() => nextDb.prepare(worktreeReferenceQuery(nextDb)).all())();
+ rows.push(...references.map((row) => ({ ...row, currentDatabase: isCurrent })));
+ } finally {
+ if (!isCurrent) nextDb.close();
+ }
+ }
+ const after = fs.readdirSync(root).filter((name) => /^(?:cindy|xdt)-.+\.db$/.test(name));
+ if (after.length !== names.length || after.some((name) => !names.includes(name))) {
+ throw new Error('task database catalog changed during scan');
+ }
+ return rows;
+}
+
const LOCAL_DUPLICATE_WINDOW_MS = 5 * 60 * 1000;
const MAX_ATTEMPTS = 5;
const RETRY_BACKOFF_MS = [1000, 5000, 30000, 5 * 60000, 30 * 60000];
@@ -2281,6 +2333,8 @@ function invalidArgs(message) {
async function dispatch(op, args) {
const readyDb = requireReadyDb();
switch (op) {
+ case 'worktreeReferences':
+ return readLocalWorktreeReferences();
case 'query': {
const { sql, params } = args || {};
return readyDb.prepare(sql).all(...normalizeParams(params));
diff --git a/apps/desktop/src/main/localDb/client/__tests__/WorkerThreadTransport.test.ts b/apps/desktop/src/main/localDb/client/__tests__/WorkerThreadTransport.test.ts
index aa0161a8be..216dfd320c 100644
--- a/apps/desktop/src/main/localDb/client/__tests__/WorkerThreadTransport.test.ts
+++ b/apps/desktop/src/main/localDb/client/__tests__/WorkerThreadTransport.test.ts
@@ -13,6 +13,37 @@ import {
import { WorkerThreadTransport } from '../WorkerThreadTransport.js';
describe('WorkerThreadTransport', () => {
+ it('supports worktree reference reads in the inline worker fallback', async () => {
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'xdt-db-worker-refs-'));
+ const drizzleDir = path.join(dir, 'drizzle');
+ const dbPath = path.join(dir, 'xdt-inline.db');
+ let transport: WorkerThreadTransport | undefined;
+ try {
+ fs.mkdirSync(drizzleDir);
+ fs.writeFileSync(path.join(drizzleDir, '0000_init.sql'), 'CREATE TABLE migration_meta (key TEXT PRIMARY KEY, value TEXT NOT NULL);', 'utf8');
+ createMigratedSmokeDb(dbPath);
+ const Database = (await import('better-sqlite3')).default;
+ const db = new Database(dbPath);
+ db.exec('CREATE TABLE sessions (id TEXT PRIMARY KEY, status TEXT, working_dir TEXT, worktree_path TEXT, source TEXT, remote_host_id TEXT)');
+ db.prepare('INSERT INTO sessions (id, status, working_dir, worktree_path, source, remote_host_id) VALUES (?, ?, ?, ?, ?, NULL)')
+ .run('session-one', 'archived', 'C:/repo/.cindy-worktrees/one', 'C:/repo/.cindy-worktrees/one', 'desktop');
+ db.close();
+ transport = new WorkerThreadTransport({
+ useInlineWorker: true,
+ dbPath,
+ drizzleDir,
+ betterSqliteModulePath: require.resolve('better-sqlite3'),
+ });
+ await expect(transport.send('worktreeReferences')).resolves.toEqual([{
+ id: 'session-one', status: 'archived', source: 'desktop',
+ workingDir: 'C:/repo/.cindy-worktrees/one', worktreePath: 'C:/repo/.cindy-worktrees/one', currentDatabase: true,
+ }]);
+ } finally {
+ if (transport) await transport.close();
+ fs.rmSync(dir, { recursive: true, force: true });
+ }
+ });
+
it('round-trips RPC messages', async () => {
const transport = new WorkerThreadTransport({ useInlineWorker: true });
try {