The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodeUtil from "node:util"; |
| 2 | |
| 3 | import { type ProjectSettingsOverrides } from "@t3tools/contracts"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | |
| 6 | import * as ProjectStore from "../orchestration-v2/ProjectStore.ts"; |
| 7 | import * as Settings from "../serverSettings.ts"; |
| 8 | |
| 9 | export function deletedProjectOverridePatch(entry: ProjectSettingsOverrides | undefined) { |
| 10 | // Keep cleanup policy for deleted threads; omitting providerInstances would preserve obsolete references in the merge. |
| 11 | return entry?.worktreeCleanup !== undefined |
| 12 | ? { worktreeCleanup: entry.worktreeCleanup, providerInstances: {} } |
| 13 | : null; |
| 14 | } |
| 15 | |
| 16 | export const sweep = Effect.gen(function* () { |
| 17 | const serverSettings = yield* Settings.ServerSettingsService; |
| 18 | const projectStore = yield* ProjectStore.ProjectStoreV2; |
| 19 | |
| 20 | const settings = yield* serverSettings.getSettings; |
| 21 | const projects = yield* projectStore.list({ includeDeleted: true }); |
| 22 | const projectsById = new Map<string, ProjectStore.ProjectRow>( |
| 23 | projects.map((project) => [project.projectId, project]), |
| 24 | ); |
| 25 | |
| 26 | const overrides = Object.entries(settings.projectSettingsOverrides).flatMap( |
| 27 | ([projectId, entry]) => { |
| 28 | const project = projectsById.get(projectId); |
| 29 | if (project?.deletedAt === null) return []; |
| 30 | const patch = project ? deletedProjectOverridePatch(entry) : null; |
| 31 | if (NodeUtil.isDeepStrictEqual(entry, patch)) return []; |
| 32 | return [[projectId, patch] as const]; |
| 33 | }, |
| 34 | ); |
| 35 | if (overrides.length === 0) return; |
| 36 | |
| 37 | yield* serverSettings.updateSettings({ |
| 38 | projectSettingsOverrides: Object.fromEntries(overrides), |
| 39 | }); |
| 40 | }).pipe( |
| 41 | Effect.asVoid, |
| 42 | Effect.catchCause((cause) => |
| 43 | Effect.logWarning("Failed to sweep orphaned project settings overrides.", cause), |
| 44 | ), |
| 45 | ); |