fix(worktree): address PR review — schema guard, startup adoption, query parity
Addresses six CodeRabbit/Greptile findings on PR #2052: - Schema guard in adoptMergedWorktrees probes for merged_into_project columns before preparing statements; returns early when absent so first boot after upgrade (pre-migration) doesn't silently fail. - Startup adoption now iterates distinct cwds from pending_messages and dedupes via resolveMainRepoPath — the worker daemon runs with cwd=plugin scripts dir, so process.cwd() fallback was a no-op. - ObservationCompiler single-project queries (queryObservations / querySummaries) OR merged_into_project into WHERE so injected context surfaces adopted worktree rows, matching the Multi variants. - SessionStore constructor now calls ensureMergedIntoProjectColumns so bundled artifacts (context-generator.cjs) that embed SessionStore get the merged_into_project column on DBs that only went through the bundled migration chain. - OBSERVER_SESSIONS_PROJECT constant is now derived from basename(OBSERVER_SESSIONS_DIR) and used across PaginationHelper, SessionStore, and timeline queries instead of hardcoded strings. - Corrected misleading Chroma retry docstring in WorktreeAdoption to match actual behavior (no auto-retry once SQL commits).
This commit is contained in:
@@ -130,8 +130,12 @@ function listMergedBranches(mainRepo: string): Set<string> {
|
||||
* Idempotent: a row is only touched when its `merged_into_project IS NULL`.
|
||||
*
|
||||
* Chroma is patched AFTER SQL commits. Chroma failure does NOT roll back SQL —
|
||||
* SQL is source of truth; a subsequent run will retry the Chroma patch because
|
||||
* the filter in `updateMergedIntoProject` keys on `sqlite_id`.
|
||||
* SQL is source of truth. A transient Chroma failure does NOT auto-retry:
|
||||
* once SQL commits, `merged_into_project IS NULL` no longer matches those rows,
|
||||
* so the same adoption pass won't rediscover them. If Chroma patching fails,
|
||||
* `result.chromaFailed` reflects the count — callers should surface this to
|
||||
* the operator, and re-running adoption after clearing `merged_into_project`
|
||||
* (or reseeding Chroma) is the recovery path.
|
||||
*/
|
||||
export async function adoptMergedWorktrees(opts: {
|
||||
repoPath?: string;
|
||||
@@ -201,6 +205,29 @@ export async function adoptMergedWorktrees(opts: {
|
||||
const { Database } = require('bun:sqlite') as typeof import('bun:sqlite');
|
||||
db = new Database(dbPath);
|
||||
|
||||
// Schema guard: adoption may be invoked on worker startup before
|
||||
// DatabaseManager runs migrations. If the `merged_into_project` column
|
||||
// isn't present yet, prepared statements below will fail with
|
||||
// "no such column", silently skipping adoption until the next restart.
|
||||
// Return early so the next boot (post-migration) picks this up.
|
||||
interface ColumnInfo { name: string }
|
||||
const obsColumns = db
|
||||
.prepare('PRAGMA table_info(observations)')
|
||||
.all() as ColumnInfo[];
|
||||
const sumColumns = db
|
||||
.prepare('PRAGMA table_info(session_summaries)')
|
||||
.all() as ColumnInfo[];
|
||||
const obsHasColumn = obsColumns.some(c => c.name === 'merged_into_project');
|
||||
const sumHasColumn = sumColumns.some(c => c.name === 'merged_into_project');
|
||||
if (!obsHasColumn || !sumHasColumn) {
|
||||
logger.debug(
|
||||
'SYSTEM',
|
||||
'Worktree adoption skipped (merged_into_project column missing; will run after migration)',
|
||||
{ obsHasColumn, sumHasColumn }
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
const selectObs = db.prepare(
|
||||
'SELECT id FROM observations WHERE project = ? AND merged_into_project IS NULL'
|
||||
);
|
||||
@@ -290,3 +317,83 @@ export async function adoptMergedWorktrees(opts: {
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run adoption once per distinct parent repo referenced by recorded cwds.
|
||||
*
|
||||
* Worker startup adoption cannot use `process.cwd()` as a seed — the daemon is
|
||||
* spawned with cwd=marketplace-plugin-dir, which isn't a git repo. Instead, we
|
||||
* derive candidate parent repos from `pending_messages.cwd` (the user's actual
|
||||
* working directories), dedupe via `resolveMainRepoPath`, and run adoption
|
||||
* against each. Failures on individual repos are logged but don't short-circuit
|
||||
* the others.
|
||||
*
|
||||
* Safe to call before `dbManager.initialize()`: opens its own short-lived DB
|
||||
* handle (readonly) to enumerate cwds, then delegates to `adoptMergedWorktrees`
|
||||
* which opens its own writable handle.
|
||||
*/
|
||||
export async function adoptMergedWorktreesForAllKnownRepos(opts: {
|
||||
dataDirectory?: string;
|
||||
dryRun?: boolean;
|
||||
} = {}): Promise<AdoptionResult[]> {
|
||||
const dataDirectory = opts.dataDirectory ?? DEFAULT_DATA_DIR;
|
||||
const dbPath = path.join(dataDirectory, 'claude-mem.db');
|
||||
const results: AdoptionResult[] = [];
|
||||
|
||||
if (!existsSync(dbPath)) {
|
||||
logger.debug('SYSTEM', 'Worktree adoption skipped (no DB yet)', { dbPath });
|
||||
return results;
|
||||
}
|
||||
|
||||
const uniqueParents = new Set<string>();
|
||||
let db: import('bun:sqlite').Database | null = null;
|
||||
try {
|
||||
const { Database } = require('bun:sqlite') as typeof import('bun:sqlite');
|
||||
db = new Database(dbPath, { readonly: true });
|
||||
|
||||
const hasPending = db.prepare(
|
||||
"SELECT name FROM sqlite_master WHERE type='table' AND name='pending_messages'"
|
||||
).get() as { name: string } | undefined;
|
||||
if (!hasPending) {
|
||||
logger.debug('SYSTEM', 'Worktree adoption skipped (pending_messages table missing)');
|
||||
return results;
|
||||
}
|
||||
|
||||
const cwdRows = db.prepare(`
|
||||
SELECT cwd FROM pending_messages
|
||||
WHERE cwd IS NOT NULL AND cwd != ''
|
||||
GROUP BY cwd
|
||||
`).all() as Array<{ cwd: string }>;
|
||||
|
||||
for (const { cwd } of cwdRows) {
|
||||
const mainRepo = resolveMainRepoPath(cwd);
|
||||
if (mainRepo) uniqueParents.add(mainRepo);
|
||||
}
|
||||
} finally {
|
||||
db?.close();
|
||||
}
|
||||
|
||||
if (uniqueParents.size === 0) {
|
||||
logger.debug('SYSTEM', 'Worktree adoption found no known parent repos');
|
||||
return results;
|
||||
}
|
||||
|
||||
for (const repoPath of uniqueParents) {
|
||||
try {
|
||||
const result = await adoptMergedWorktrees({
|
||||
repoPath,
|
||||
dataDirectory,
|
||||
dryRun: opts.dryRun
|
||||
});
|
||||
results.push(result);
|
||||
} catch (err) {
|
||||
logger.warn(
|
||||
'SYSTEM',
|
||||
'Worktree adoption failed for parent repo (continuing)',
|
||||
{ repoPath, error: err instanceof Error ? err.message : String(err) }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user