| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- /* path 规则集中维护:旧 task 快照与 provider 配置路径仍在这里收口。 */
- import { lstatSync } from "node:fs";
- import { cp } from "node:fs/promises";
- import { createHash } from "node:crypto";
- import { basename, join, win32 } from "node:path";
- import { homedir } from "node:os";
- import {
- DATA_BASE_DIR_FORBIDDEN_WINDOWS_INSTALL_DIR_ERROR_CODE,
- ZCODE_AGENT_RUNTIME,
- } from "@zcode/shared";
- let _dataBaseDir: string | null = null;
- export const ZCODE_WINDOWS_APP_INSTALL_DIR_ENV = "ZCODE_WINDOWS_APP_INSTALL_DIR";
- const envDataBaseDir = process.env.ZCODE_DATA_BASE_DIR?.trim() || null;
- const defaultDataBaseDir = process.env.HOME?.trim() || homedir();
- export interface DataBaseDirTargetValidationOptions {
- platform?: NodeJS.Platform | string;
- env?: Record<string, string | undefined>;
- appInstallDir?: string | null;
- }
- export type DataBaseDirTargetValidationResult =
- | { ok: true }
- | {
- ok: false;
- code: typeof DATA_BASE_DIR_FORBIDDEN_WINDOWS_INSTALL_DIR_ERROR_CODE;
- forbiddenDir: string;
- };
- /** Set the base directory for app data (replaces homedir() prefix). */
- export function setDataBaseDir(dir: string | null): void {
- _dataBaseDir = dir?.trim() || null;
- }
- /** Get the current base directory. Priority: setDataBaseDir() > env ZCODE_DATA_BASE_DIR > homedir(). */
- export function getDataBaseDir(): string {
- if (_dataBaseDir) return _dataBaseDir;
- if (envDataBaseDir) return envDataBaseDir;
- // 服务实例会启动后台刷新任务;若每次调用都动态读取 HOME,
- // 测试或宿主切换环境变量后,旧实例可能把数据写到新实例目录。
- return defaultDataBaseDir;
- }
- /** {dataBaseDir}/.zcode */
- export function getZCodeDataRootDir(): string {
- return join(getDataBaseDir(), ".zcode");
- }
- /** 非项目对话共享的真实工作目录;默认 ~/.zcode/workspace/default。 */
- export function getConversationWorkspaceDir(): string {
- return join(getZCodeDataRootDir(), "workspace", "default");
- }
- /** {dataBaseDir}/.zcode/v2 */
- export function getAppConfigDir(): string {
- return join(getZCodeDataRootDir(), "v2");
- }
- function readEnvValue(env: Record<string, string | undefined>, key: string): string | undefined {
- const direct = env[key]?.trim();
- if (direct) {
- return direct;
- }
- const lowerKey = key.toLowerCase();
- for (const [candidateKey, value] of Object.entries(env)) {
- if (candidateKey.toLowerCase() !== lowerKey) {
- continue;
- }
- const trimmed = value?.trim();
- if (trimmed) {
- return trimmed;
- }
- }
- return undefined;
- }
- function normalizeWindowsComparablePath(pathValue: string): string | null {
- const trimmed = pathValue.trim();
- if (!trimmed) {
- return null;
- }
- const normalized = win32.normalize(trimmed).replace(/[\\/]+$/, "");
- if (!normalized) {
- return null;
- }
- return win32
- .resolve(normalized)
- .replace(/[\\/]+$/, "")
- .toLowerCase();
- }
- function isWindowsPathEqualOrInside(pathValue: string, rootValue: string): boolean {
- const normalizedPath = normalizeWindowsComparablePath(pathValue);
- const normalizedRoot = normalizeWindowsComparablePath(rootValue);
- if (!normalizedPath || !normalizedRoot) {
- return false;
- }
- return normalizedPath === normalizedRoot || normalizedPath.startsWith(`${normalizedRoot}\\`);
- }
- function collectWindowsForbiddenAppInstallDirs(
- options: Required<Pick<DataBaseDirTargetValidationOptions, "env">> &
- Pick<DataBaseDirTargetValidationOptions, "appInstallDir">,
- ): string[] {
- const env = options.env;
- const programFiles = readEnvValue(env, "ProgramFiles");
- const programFilesX86 = readEnvValue(env, "ProgramFiles(x86)");
- const programW6432 = readEnvValue(env, "ProgramW6432");
- const localAppData = readEnvValue(env, "LOCALAPPDATA");
- const candidates = [
- options.appInstallDir,
- readEnvValue(env, ZCODE_WINDOWS_APP_INSTALL_DIR_ENV),
- programFiles ? win32.join(programFiles, "ZCode") : null,
- programFilesX86 ? win32.join(programFilesX86, "ZCode") : null,
- programW6432 ? win32.join(programW6432, "ZCode") : null,
- localAppData ? win32.join(localAppData, "Programs", "ZCode") : null,
- ];
- const seen = new Set<string>();
- const result: string[] = [];
- for (const candidate of candidates) {
- const normalized =
- typeof candidate === "string" ? normalizeWindowsComparablePath(candidate) : null;
- if (!candidate || !normalized || seen.has(normalized)) {
- continue;
- }
- seen.add(normalized);
- result.push(candidate);
- }
- return result;
- }
- export function validateDataBaseDirTarget(
- targetBaseDir: string,
- options: DataBaseDirTargetValidationOptions = {},
- ): DataBaseDirTargetValidationResult {
- if ((options.platform ?? process.platform) !== "win32") {
- return { ok: true };
- }
- for (const forbiddenDir of collectWindowsForbiddenAppInstallDirs({
- env: options.env ?? process.env,
- appInstallDir: options.appInstallDir ?? null,
- })) {
- if (isWindowsPathEqualOrInside(targetBaseDir, forbiddenDir)) {
- return {
- ok: false,
- code: DATA_BASE_DIR_FORBIDDEN_WINDOWS_INSTALL_DIR_ERROR_CODE,
- forbiddenDir,
- };
- }
- }
- return { ok: true };
- }
- export function getExportLogStageDir(): string {
- return join(getZCodeDataRootDir(), "export-log-stage");
- }
- export function getExportLogDir(): string {
- return join(getZCodeDataRootDir(), "export-log");
- }
- export function getFeedbackRootDir(): string {
- return join(getZCodeDataRootDir(), "feedback");
- }
- export function getFeedbackAttachmentDir(): string {
- return join(getFeedbackRootDir(), "attachments");
- }
- export function getFeedbackLogArchiveDir(): string {
- return join(getFeedbackRootDir(), "logs");
- }
- export function getGitCheckpointIndexRootDir(): string {
- return join(getZCodeDataRootDir(), "git-checkpoint-index");
- }
- /** ~/.zcode/v2/tasks-index.sqlite */
- export function getTasksIndexDatabasePath(): string {
- return join(getAppConfigDir(), "tasks-index.sqlite");
- }
- /** workspace 级身份键:远程优先使用 workspaceIdentity,本地回退 workspacePath。 */
- function getWorkspaceKey(workspacePath: string, workspaceIdentity?: string): string {
- return workspaceIdentity?.trim() || workspacePath;
- }
- /** 与 ZCode session 持久化一致:使用 workspaceKey 的 SHA-256 前 12 位 */
- export function getWorkspaceHash(workspacePath: string, workspaceIdentity?: string): string {
- return createHash("sha256")
- .update(getWorkspaceKey(workspacePath, workspaceIdentity))
- .digest("hex")
- .slice(0, 12);
- }
- /** ~/.zcode/v2/sessions/{workspaceHash} */
- function getTaskSessionDir(workspacePath: string, workspaceIdentity?: string): string {
- return join(getAppConfigDir(), "sessions", getWorkspaceHash(workspacePath, workspaceIdentity));
- }
- /** ~/.zcode/v2/sessions/{workspaceHash}/{taskId}.json */
- export function getLegacyTaskSessionSnapshotPath(
- workspacePath: string,
- taskId: string,
- workspaceIdentity?: string,
- ): string {
- return join(getTaskSessionDir(workspacePath, workspaceIdentity), `${taskId}.json`);
- }
- /** ~/.zcode/v2/sessions/{workspaceHash}/{taskId}.deleted.json */
- export function getLegacyDeletedTaskSessionSnapshotPath(
- workspacePath: string,
- taskId: string,
- workspaceIdentity?: string,
- ): string {
- return join(getTaskSessionDir(workspacePath, workspaceIdentity), `${taskId}.deleted.json`);
- }
- /**
- * GLM(唯一 agent provider)的配置目录:~/.zcode/cli。
- * Bugfix:GLM 配置改为直接读写新版 zcode-cli 的全局配置目录,不做 workspace 隔离,
- * 避免同机不同工作区配置漂移。
- */
- export function getProviderWorkspaceConfigDir(): string {
- return ZCODE_AGENT_RUNTIME.nativeConfigDir
- ? join(getDataBaseDir(), ZCODE_AGENT_RUNTIME.nativeConfigDir)
- : join(getDataBaseDir(), ".zcode", "cli");
- }
- /**
- * Copy the .zcode/v2 data directory from one base dir to another.
- * Excludes setting.json and its transient atomic-write siblings — bootstrap
- * state must only live at the default homedir location.
- */
- export async function copyDataDirectory(oldBaseDir: string, newBaseDir: string): Promise<void> {
- const oldDir = join(oldBaseDir, ".zcode", "v2");
- const newDir = join(newBaseDir, ".zcode", "v2");
- await cp(oldDir, newDir, {
- recursive: true,
- force: false,
- filter: (source) => {
- const sourceName = basename(source);
- if (sourceName === "setting.json" || sourceName.startsWith("setting.json.")) {
- // setting.json.lock 和 setting.json.*.tmp 由原子写入短暂创建/删除,
- // 复制过程中扫描到已消失的 lock 会触发 ENOENT,并让数据目录迁移失败。
- // 这些文件都属于 bootstrap 写入中间态,不能迁移到新数据根。
- return false;
- }
- // Windows 非提权环境下 fs.cp 无法复制符号链接(EPERM)。
- // 跳过符号链接可避免 Windows 非提权环境下 fs.cp 报 EPERM。
- try {
- if (lstatSync(source).isSymbolicLink()) return false;
- } catch {
- // lstat 失败时放行,让 cp 自行处理
- }
- return true;
- },
- });
- }
|