MCPcopy Create free account
hub / github.com/cortexkit/magic-context / sweepProject

Function sweepProject

packages/plugin/src/plugin/dream-timer.ts:266–392  ·  view source on GitHub ↗

* Run all per-project maintenance for one registration: git commit indexing * (when enabled) plus dream schedule check + queue processing (when enabled). * * Each registered project gets its own pass per tick — Desktop loads the * plugin once per project in the same process, and every project ne

(
    reg: ProjectRegistration,
    origin: "startup" | "interval",
    db: Database,
    gitCommitEnabled?: boolean,
)

Source from the content-addressed store, hash-verified

264 * own commits indexed and its own dream schedule honored.
265 */
266async function sweepProject(
267 reg: ProjectRegistration,
268 origin: "startup" | "interval",
269 db: Database,
270 gitCommitEnabled?: boolean,
271): Promise<void> {
272 // Dead-directory guard: a registration whose directory no longer exists
273 // (e.g. a finalized mason worktree) can't have meaningful dreamer work —
274 // git indexing + key-files/verify would ENOENT reading from the gone path.
275 // Skip it and unregister so it stops being swept. For a `dir:` identity
276 // (path-unique → truly orphaned once the path is gone) also GC its schedule
277 // rows; a `git:` identity is SHARED across worktrees/clones, so a single dead
278 // worktree must NOT delete the shared project's schedule.
279 if (!directoryStillExists(reg.directory)) {
280 log(
281 `[dreamer] project directory no longer exists (${reg.projectIdentity}); skipping + unregistering`,
282 );
283 if (reg.projectIdentity.startsWith("dir:")) {
284 try {
285 const removed = deleteTaskScheduleRowsForProject(db, reg.projectIdentity);
286 if (removed > 0) {
287 log(
288 `[dreamer] GC'd ${removed} orphaned schedule row(s) for ${reg.projectIdentity}`,
289 );
290 }
291 } catch (error) {
292 log(`[dreamer] orphan schedule GC failed for ${reg.projectIdentity}:`, error);
293 }
294 }
295 registeredProjects.delete(reg.directory);
296 return;
297 }
298
299 await reg.ensureRegistered(reg.directory, db);
300 const embeddingSnapshot = getProjectEmbeddingSnapshot(reg.projectIdentity);
301 const commitIndexingEnabled = gitCommitEnabled ?? embeddingSnapshot?.gitCommitEnabled === true;
302 const gc = sweepStaleEmbeddingIdentitiesForProject(db, reg.projectIdentity);
303 const gcDeleted = gc.memoryRowsDeleted + gc.commitRowsDeleted + gc.chunkRowsDeleted;
304 if (gcDeleted > 0) {
305 log(
306 `[magic-context] GC'd ${gcDeleted} stale embedding row(s) for ${reg.projectIdentity} ` +
307 `(memory=${gc.memoryRowsDeleted} commit=${gc.commitRowsDeleted} chunk=${gc.chunkRowsDeleted})`,
308 );
309 }
310
311 const dreamerConfig = reg.dreamerConfig;
312 const dreamingEnabled = Boolean(dreamerConfig && dreamerConfig.disable !== true);
313 if (commitIndexingEnabled && reg.gitCommitIndexing) {
314 await sweepGitCommits({
315 directory: reg.directory,
316 gitCommitIndexing: reg.gitCommitIndexing,
317 projectIdentity: reg.projectIdentity,
318 db,
319 });
320 }
321
322 if (!dreamingEnabled || !dreamerConfig) {
323 return;

Callers 2

startDreamScheduleTimerFunction · 0.85
runTickFunction · 0.85

Calls 15

logFunction · 0.90
createDreamTaskExecutorFunction · 0.90
runDueTasksForProjectFunction · 0.90
openOpenCodeDbFunction · 0.90
closeQuietlyFunction · 0.90

Tested by

no test coverage detected