mirror of
https://github.com/makecindy/cindy.git
synced 2026-12-03 21:40:27 +08:00
* fix(dev): 新 worktree 装完依赖即可用,开发版启动失败能看到原因 - postinstall 末尾复核 Electron dist,缺失时补装:pnpm side-effects cache 记下「electron 已构建但无产物」后,同机新 worktree 都会跳过 Electron 自己的 postinstall - workspace 测试开跑前只读检查依赖与 Electron,未就绪直接给出处置命令 - macOS 无 TTY 启动时用 script(1) 把 dev 终端输出写入 apps/desktop/logs/dev-terminal.log,失败时打印日志末尾并在 verdict 带 log= - USERDATA_IN_USE 给出改用本 worktree 沙箱的 next - 进程归属按最深 worktree 判定,主 checkout restart 不再误杀 .cindy-worktrees 下的实例 - 同步环境准备、Desktop 启动与 worktree 会话文档 Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com> * fix(dev): 收紧依赖预检范围,终端日志限长并覆盖所有失败结论 - 依赖预检只在选中 Desktop 时检查 Electron,并把缺失的 node_modules/electron 判为安装不完整 - 终端日志经命名管道只保留前 8MB,长期运行的 dev 不再让文件无限增长; 环境变量前缀紧贴 script,cd 失败时整组命令不执行 - ready 后进程退出导致的 WHOAMI_MISMATCH 也打印日志末尾并带 log= - ensure-deps 测试去掉 CI 继承的 ELECTRON_OVERRIDE_DIST_PATH Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com> * test(dev): 终端日志命令断言按平台路径拼接 Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com> --------- Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com>
226 lines
7.8 KiB
JavaScript
226 lines
7.8 KiB
JavaScript
import { createHash } from 'node:crypto';
|
|
import path from 'node:path';
|
|
import { resolveDesktopDevRegion } from './shared/desktop-dev-region.mjs';
|
|
|
|
export const DESKTOP_DEV_VERDICT_PREFIX = 'DESKTOP_DEV_VERDICT';
|
|
export const WORKTREE_ISOLATED_ARG = '--isolated=@worktree';
|
|
export const SHARED_USERDATA_ARG = '--shared';
|
|
export const DEFAULT_ISOLATED_ARG = '--isolated=dev';
|
|
const WORKTREE_NAME_DIGEST_LEN = 6;
|
|
|
|
const VERDICT_FIELDS = Object.freeze([
|
|
'code',
|
|
'mode',
|
|
'sandbox',
|
|
'root',
|
|
'commit',
|
|
'pid',
|
|
'region',
|
|
'message',
|
|
'log',
|
|
'next',
|
|
]);
|
|
|
|
const SHARED_FAILURE_CODES_WITH_ISOLATED_NEXT = new Set([
|
|
'MIGRATION_POLICY',
|
|
'PRESERVE_RUNNING_INCOMPATIBLE',
|
|
'STARTUP_TIMEOUT',
|
|
'MIGRATE_FAILED',
|
|
'WHOAMI_MISMATCH',
|
|
'STARTUP_FAILED',
|
|
'DEV_PROCESS_EXITED',
|
|
'AUTH_INIT_FAILED',
|
|
'HOSTED_SHARED_REFUSED',
|
|
]);
|
|
|
|
function normalizeRoot(value) {
|
|
return path.resolve(value).replaceAll('\\', '/').toLowerCase();
|
|
}
|
|
|
|
function worktreeHashKey(rootDir, foldCase) {
|
|
const resolved = path.resolve(rootDir).replaceAll('\\', '/');
|
|
return foldCase ? resolved.toLowerCase() : resolved;
|
|
}
|
|
|
|
function singleLine(value) {
|
|
return String(value).replace(/\s+/g, ' ').trim();
|
|
}
|
|
|
|
export function isolationNameFromWorktree(rootDir, { foldCase } = {}) {
|
|
const resolved = path.resolve(rootDir);
|
|
let name = path.basename(resolved);
|
|
name = name.replace(/^cindy-/i, '');
|
|
name = name.replace(/[^A-Za-z0-9_-]+/g, '-').replace(/^-+|-+$/g, '');
|
|
if (!name) name = 'worktree';
|
|
const shouldFold = foldCase ?? process.platform !== 'linux';
|
|
const digest = createHash('sha256')
|
|
.update(worktreeHashKey(resolved, shouldFold))
|
|
.digest('hex')
|
|
.slice(0, WORKTREE_NAME_DIGEST_LEN);
|
|
const maxBase = 32 - 1 - digest.length;
|
|
const base = name.slice(0, maxBase).replace(/-+$/g, '') || 'wt';
|
|
return `${base}-${digest}`;
|
|
}
|
|
|
|
export function isolatedRestartNextCommand({ local = false, region } = {}) {
|
|
const script = local ? 'restart:desktop:local' : 'restart:desktop:remote';
|
|
const regionArg = region && region !== 'global' ? ` --region=${region}` : '';
|
|
return `pnpm ${script}${regionArg} -- --isolated=@worktree`;
|
|
}
|
|
|
|
export const ISOLATED_RESTART_NEXT = isolatedRestartNextCommand();
|
|
|
|
export function restartContextFromArgv(argv = [], env = process.env) {
|
|
let region;
|
|
try {
|
|
region = resolveDesktopDevRegion(argv, env);
|
|
} catch {
|
|
region = undefined;
|
|
}
|
|
return {
|
|
isolated: argv.some((arg) => arg === '--isolated' || arg.startsWith('--isolated='))
|
|
|| env.XDT_ISOLATED === '1',
|
|
local: argv.includes('--local'),
|
|
region,
|
|
};
|
|
}
|
|
|
|
export function resolveIsolatedArg(isolatedArg, rootDir, options) {
|
|
if (isolatedArg === WORKTREE_ISOLATED_ARG) {
|
|
return `--isolated=${isolationNameFromWorktree(rootDir, options)}`;
|
|
}
|
|
return isolatedArg;
|
|
}
|
|
|
|
/**
|
|
* Dev restart defaults to one stable isolated sandbox shared by every
|
|
* checkout. The legacy official profile remains opt-in via --shared.
|
|
*/
|
|
export function normalizeDesktopRestartArgv(argv = [], env = process.env) {
|
|
if (argv.some((arg) => arg === '--isolated' || arg.startsWith('--isolated='))) {
|
|
return argv;
|
|
}
|
|
if (env.XDT_ISOLATED === '1') return argv;
|
|
if (argv.includes(SHARED_USERDATA_ARG) || argv.includes('--preserve-running')) {
|
|
return argv;
|
|
}
|
|
return [...argv, DEFAULT_ISOLATED_ARG];
|
|
}
|
|
|
|
export function desktopRestartArgvConflictMessage(argv = [], env = process.env) {
|
|
if (!argv.includes(SHARED_USERDATA_ARG)) return null;
|
|
if (
|
|
argv.some((arg) => arg === '--isolated' || arg.startsWith('--isolated='))
|
|
|| env.XDT_ISOLATED === '1'
|
|
) {
|
|
return '--shared cannot be combined with --isolated or XDT_ISOLATED=1';
|
|
}
|
|
return null;
|
|
}
|
|
|
|
export function shouldSuggestIsolatedNext({ isolated, code } = {}) {
|
|
return !isolated && SHARED_FAILURE_CODES_WITH_ISOLATED_NEXT.has(code);
|
|
}
|
|
|
|
export function inferDesktopDevFailureCode(message) {
|
|
const text = String(message ?? '');
|
|
const tagged = text.match(/\[([A-Z][A-Z0-9_]*)\]/);
|
|
if (tagged) return tagged[1];
|
|
if (/cannot run migration artifacts/i.test(text)) return 'MIGRATION_POLICY';
|
|
if (/already in use by another checkout/i.test(text)) return 'USERDATA_IN_USE';
|
|
if (/cannot share official userData while hosted/i.test(text)) return 'HOSTED_SHARED_REFUSED';
|
|
if (/Refusing to restart from within|running inside an Cindy desktop dev process tree/i.test(text)) {
|
|
return 'HOSTED_RESTART_REFUSED';
|
|
}
|
|
if (/--preserve-running cannot/i.test(text)) return 'PRESERVE_RUNNING_INCOMPATIBLE';
|
|
if (/--isolated cannot use the official/i.test(text)) return 'ISOLATED_OFFICIAL_PROFILE';
|
|
if (/did not finish window\/auth\/database startup/i.test(text)) return 'STARTUP_TIMEOUT';
|
|
if (/Invalid --isolated name/i.test(text)) return 'INVALID_ISOLATED_NAME';
|
|
if (/root\/commit\/ready did not match|Desktop dev process is not running/i.test(text)) {
|
|
return 'WHOAMI_MISMATCH';
|
|
}
|
|
if (/failed with exit/i.test(text)) return 'STARTUP_FAILED';
|
|
return 'STARTUP_FAILED';
|
|
}
|
|
|
|
export function formatDesktopDevVerdict(verdict) {
|
|
const state = verdict?.state === 'ready' ? 'ready' : 'failed';
|
|
const lines = [`${DESKTOP_DEV_VERDICT_PREFIX}=${state}`];
|
|
for (const key of VERDICT_FIELDS) {
|
|
const value = verdict?.[key];
|
|
if (value == null || value === '') continue;
|
|
lines.push(`${key}=${singleLine(value)}`);
|
|
}
|
|
return `${lines.join('\n')}\n`;
|
|
}
|
|
|
|
export function printDesktopDevVerdict(verdict, writer = console.log) {
|
|
writer(formatDesktopDevVerdict(verdict).trimEnd());
|
|
}
|
|
|
|
function isolatedNextIfNeeded(context, code) {
|
|
// 默认 dev 沙箱跨 checkout 共用:被另一个 worktree 的实例占着时,换成本 worktree
|
|
// 自己的沙箱就能并行启动,不必停掉别人的实例。
|
|
return code === 'USERDATA_IN_USE' || shouldSuggestIsolatedNext({ isolated: context.isolated, code })
|
|
? { next: isolatedRestartNextCommand(context) }
|
|
: {};
|
|
}
|
|
|
|
export function buildDesktopDevVerdictFromWhoami(report, context = {}) {
|
|
const expectedRoot = report?.expected?.rootDir;
|
|
const expectedCommit = report?.expected?.commit ?? null;
|
|
const instances = Array.isArray(report?.instances) ? report.instances : [];
|
|
const matched = instances.find((instance) =>
|
|
expectedRoot
|
|
&& normalizeRoot(instance.rootDir) === normalizeRoot(expectedRoot)
|
|
&& instance.ready === true
|
|
&& instance.commitVerified === true
|
|
&& instance.commit === expectedCommit);
|
|
|
|
if (report?.match && matched) {
|
|
return {
|
|
state: 'ready',
|
|
mode: matched.isolated === true || context.isolated ? 'isolated' : 'shared',
|
|
...(context.sandbox ? { sandbox: context.sandbox } : {}),
|
|
root: expectedRoot,
|
|
commit: expectedCommit,
|
|
pid: matched.pid,
|
|
...(matched.region ? { region: matched.region } : {}),
|
|
};
|
|
}
|
|
|
|
const code = 'WHOAMI_MISMATCH';
|
|
return {
|
|
state: 'failed',
|
|
code,
|
|
message: instances.length === 0
|
|
? 'Desktop dev process is not running for this checkout.'
|
|
: 'Desktop dev is running but root/commit/ready did not match this checkout.',
|
|
mode: context.isolated ? 'isolated' : 'shared',
|
|
...(context.sandbox ? { sandbox: context.sandbox } : {}),
|
|
root: expectedRoot,
|
|
commit: expectedCommit,
|
|
...isolatedNextIfNeeded(context, code),
|
|
};
|
|
}
|
|
|
|
export function buildDesktopDevVerdictFromFailure(error, context = {}) {
|
|
const status = error?.startupStatus && typeof error.startupStatus === 'object'
|
|
? error.startupStatus
|
|
: null;
|
|
const message = status?.message
|
|
|| (error instanceof Error ? error.message : String(error ?? 'Desktop dev failed to start'));
|
|
const code = context.code
|
|
|| (typeof status?.code === 'string' ? status.code : inferDesktopDevFailureCode(message));
|
|
return {
|
|
state: 'failed',
|
|
code,
|
|
message,
|
|
mode: context.isolated ? 'isolated' : 'shared',
|
|
...(context.sandbox ? { sandbox: context.sandbox } : {}),
|
|
...(context.rootDir ? { root: context.rootDir } : {}),
|
|
...(context.log ? { log: context.log } : {}),
|
|
...isolatedNextIfNeeded(context, code),
|
|
};
|
|
}
|