aodw-skill 0.10.0 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/.aodw-next/01-core/ai-knowledge-rules.md +35 -2
  2. package/.aodw-next/01-core/aodw-constitution.md +22 -3
  3. package/.aodw-next/01-core/git-discipline.md +110 -52
  4. package/.aodw-next/01-core/test-discipline.md +2 -0
  5. package/.aodw-next/02-workflow/autopilot-protocol.md +4 -1
  6. package/.aodw-next/02-workflow/environment-roles.md +106 -0
  7. package/.aodw-next/02-workflow/orca-multi-session.md +80 -0
  8. package/.aodw-next/02-workflow/rt-manager.md +83 -12
  9. package/.aodw-next/02-workflow/shared-verification-host.md +103 -0
  10. package/.aodw-next/02-workflow/sub-agent-driven-profile.md +2 -1
  11. package/.aodw-next/04-auditors/aodw-development-auditor-rules.md +2 -6
  12. package/.aodw-next/04-auditors/aodw-full-auditor-rules.md +2 -6
  13. package/.aodw-next/README.md +168 -7
  14. package/.aodw-next/SKILL.md +4 -1
  15. package/.aodw-next/manifest.yaml +26 -2
  16. package/.aodw-next/project.yaml +4 -0
  17. package/.aodw-next/skills/handover-pack/scripts/next-number.sh +1 -1
  18. package/.aodw-next/skills/handover-pack/templates/handover-template.md +3 -1
  19. package/.aodw-next/templates/dev-session-guide-template.md +20 -0
  20. package/.aodw-next/templates/environments.template.md +54 -0
  21. package/.aodw-next/templates/lessons.template.md +7 -0
  22. package/.aodw-next/templates/main-session-handover-template.md +19 -0
  23. package/.aodw-next/templates/orca-handoff-template.md +31 -0
  24. package/.aodw-next/templates/release/checklist.template.md +29 -0
  25. package/.aodw-next/templates/release/notes.template.md +22 -0
  26. package/.aodw-next/templates/release/plan.template.md +15 -0
  27. package/.aodw-next/templates/release/retro-index.template.md +8 -0
  28. package/.aodw-next/templates/release/retro-topic.template.md +33 -0
  29. package/.aodw-next/templates/rt-lite.template.md +1 -0
  30. package/.aodw-next/templates/rt-meta-template.yaml +2 -2
  31. package/.aodw-next/templates/shared-verification-host-profile.template.md +50 -0
  32. package/.aodw-next/tools/next-rt-id.sh +39 -0
  33. package/.aodw-next/tools/rel-plan.mjs +203 -0
  34. package/.aodw-next/tools/worktree-gc.sh +260 -0
  35. package/AODW_Adapters/README.md +22 -126
  36. package/AODW_Adapters/agents/AGENTS.md +11 -0
  37. package/AODW_Adapters/antigravity/.agent/rules/aodw-next.md +2 -72
  38. package/AODW_Adapters/claude/CLAUDE.md +2 -72
  39. package/AODW_Adapters/copilot/.github/copilot-instructions.md +3 -0
  40. package/AODW_Adapters/cursor/.cursor/rules/aodw-next.mdc +3 -75
  41. package/AODW_Adapters/openclaw/SKILL.md +3 -106
  42. package/README.md +60 -22
  43. package/bin/aodw.js +374 -279
  44. package/bin/commands/new.js +21 -25
  45. package/bin/commands/update.js +387 -0
  46. package/bin/commands/verify.js +420 -0
  47. package/bin/utils/lock.js +343 -0
  48. package/bin/utils/path-guard.js +449 -0
  49. package/bin/utils/release.js +36 -0
  50. package/bin/utils/tool-entries.js +506 -0
  51. package/package.json +8 -2
  52. package/.aodw-next/templates/aodw-kernel-loader-template.md +0 -71
  53. package/AODW_Adapters/cursor/.cursor/commands/README.md +0 -37
  54. package/AODW_Adapters/cursor/.cursor/rules/aodw-autopilot-continue.mdc +0 -42
  55. package/AODW_Adapters/openclaw/AGENTS.md +0 -142
  56. package/bin/processors/index.js +0 -98
  57. package/bin/update-adapters-from-template.js +0 -76
@@ -0,0 +1,449 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+
4
+ const REFUSAL_PHRASES = new Map([
5
+ ['ENOTDIR', '上级路径不是目录'],
6
+ ['EACCES', '没有读权限'],
7
+ ['EPERM', '没有读权限'],
8
+ ['ELOOP', '符号链接循环'],
9
+ ['ENAMETOOLONG', '路径过长'],
10
+ ]);
11
+
12
+ // 只归类操作系统错误;无 errno 的程序错误必须由调用方原样抛出。
13
+ export function refusalReason(err) {
14
+ if (!Number.isInteger(err?.errno)) return null;
15
+ return REFUSAL_PHRASES.get(err.code) ?? `文件系统错误 ${err.code}`;
16
+ }
17
+
18
+ export const LOCK_FILENAME = 'aodw.lock';
19
+ // init 写入的入口薄壳登记(路径与写入时的 SHA-256),同样不属于上游。
20
+ export const ENTRIES_FILENAME = 'aodw.entries.json';
21
+
22
+ // 核心目录内不属于上游、永远不登记进锁的顶层路径。
23
+ export const NON_UPSTREAM_TOP_FILES = new Set([
24
+ LOCK_FILENAME,
25
+ ENTRIES_FILENAME,
26
+ 'project.yaml',
27
+ 'config.yaml',
28
+ 'tools-status.yaml',
29
+ ]);
30
+
31
+ export const NON_UPSTREAM_TOP_DIRS = new Set([
32
+ '06-project',
33
+ ]);
34
+
35
+ export function isProtectedCorePath(relPosix) {
36
+ if (NON_UPSTREAM_TOP_FILES.has(relPosix)) return true;
37
+ for (const dir of NON_UPSTREAM_TOP_DIRS) {
38
+ if (relPosix === dir || relPosix.startsWith(`${dir}/`)) return true;
39
+ }
40
+ return false;
41
+ }
42
+
43
+ // 仅做词法校验:相对、已规范化、无 `..` 段、无反斜杠与盘符。
44
+ export function validateRelativePosixPath(relPath) {
45
+ if (typeof relPath !== 'string' || relPath.length === 0) {
46
+ return { ok: false, reason: '路径为空或非字符串' };
47
+ }
48
+ if (relPath.includes('\0') || relPath.includes('\\')) {
49
+ return { ok: false, reason: '路径包含非法字符(NUL 或反斜杠)' };
50
+ }
51
+ if (path.posix.isAbsolute(relPath) || /^[A-Za-z]:/.test(relPath)) {
52
+ return { ok: false, reason: '不允许绝对路径或盘符路径' };
53
+ }
54
+ const parts = relPath.split('/');
55
+ if (parts.some((seg) => seg === '' || seg === '.' || seg === '..')) {
56
+ return { ok: false, reason: '路径包含空段、. 或 ..' };
57
+ }
58
+ if (path.posix.normalize(relPath) !== relPath) {
59
+ return { ok: false, reason: '路径未规范化' };
60
+ }
61
+ return { ok: true };
62
+ }
63
+
64
+ // 通道目录名:仓内的相对路径即可,自定义目录名与 `..cache` 一类名称照常放行。
65
+ export function validateCoreDirName(coreDirName) {
66
+ const check = validateRelativePosixPath(coreDirName);
67
+ if (!check.ok) {
68
+ return { ok: false, reason: `核心目录配置非法 (${coreDirName}): ${check.reason}` };
69
+ }
70
+ return { ok: true };
71
+ }
72
+
73
+ export function validateLockRelPath(relPath) {
74
+ const check = validateRelativePosixPath(relPath);
75
+ if (!check.ok) return check;
76
+ if (isProtectedCorePath(relPath)) {
77
+ return { ok: false, reason: '受保护或非上游路径不得登记在锁文件中' };
78
+ }
79
+ return { ok: true };
80
+ }
81
+
82
+ // 按路径段判断 realChild 是否位于 realParent 之内;`..cache` 是合法的仓内名称。
83
+ export function isInsideDir(realParent, realChild) {
84
+ const rel = path.relative(realParent, realChild);
85
+ return rel !== '' && rel !== '..' && !rel.startsWith(`..${path.sep}`) && !path.isAbsolute(rel);
86
+ }
87
+
88
+ function isMissingEntry(err) {
89
+ return Number.isInteger(err?.errno) && (err.code === 'ENOENT' || err.code === 'ENOTDIR');
90
+ }
91
+
92
+ function walkReadFailure(err, absPath, allowMissing) {
93
+ if (isMissingEntry(err)) {
94
+ if (allowMissing) return { ok: true, absPath, exists: false };
95
+ return { ok: false, code: 'missing', absPath, reason: '路径不存在' };
96
+ }
97
+ const reason = refusalReason(err);
98
+ if (reason === null) throw err;
99
+ return { ok: false, code: 'error', absPath, reason, error: err };
100
+ }
101
+
102
+ // 自 rootDir 起逐段 lstat:已存在的每一段都不得是符号链接。
103
+ // allowMissing 为 true 时,遇到不存在(或中间段不是目录)的路径即视为其后全部不存在。
104
+ export function walkRealPath(rootDir, relPosix, { allowMissing = false } = {}) {
105
+ const check = validateRelativePosixPath(relPosix);
106
+ if (!check.ok) return { ok: false, code: 'invalid_path', reason: check.reason };
107
+
108
+ const parts = relPosix.split('/');
109
+ let cur = rootDir;
110
+ for (let i = 0; i < parts.length; i++) {
111
+ cur = path.join(cur, parts[i]);
112
+ let st;
113
+ try {
114
+ st = fs.lstatSync(cur);
115
+ } catch (err) {
116
+ return walkReadFailure(err, cur, allowMissing);
117
+ }
118
+ if (st.isSymbolicLink()) {
119
+ return { ok: false, code: 'symlink', absPath: cur, reason: '路径包含符号链接' };
120
+ }
121
+ const isLeaf = i === parts.length - 1;
122
+ if (!isLeaf && !st.isDirectory()) {
123
+ if (allowMissing) return { ok: true, absPath: cur, exists: false };
124
+ return { ok: false, code: 'not_dir', absPath: cur, reason: REFUSAL_PHRASES.get('ENOTDIR') };
125
+ }
126
+ if (isLeaf) {
127
+ try {
128
+ return { ok: true, absPath: cur, exists: true, realPath: fs.realpathSync(cur), stat: st };
129
+ } catch (err) {
130
+ return walkReadFailure(err, cur, allowMissing);
131
+ }
132
+ }
133
+ }
134
+ return { ok: true, absPath: cur, exists: true };
135
+ }
136
+
137
+ // 解析 baseDir 下的普通文件,校验不通过符号链接且真实路径位于 realBaseDir 之内。
138
+ export function resolveSafeFileInDir(baseDir, realBaseDir, relPosix, { rejectSymlinks = true } = {}) {
139
+ const res = walkRealPath(baseDir, relPosix, { allowMissing: false });
140
+ if (!res.ok) {
141
+ if (res.code === 'symlink' && !rejectSymlinks) {
142
+ let realTarget;
143
+ let finalStat;
144
+ try {
145
+ realTarget = fs.realpathSync(path.join(baseDir, ...relPosix.split('/')));
146
+ finalStat = fs.statSync(realTarget);
147
+ } catch {
148
+ return { ok: false, code: 'missing', reason: '目标不存在或符号链接失效' };
149
+ }
150
+ if (!isInsideDir(realBaseDir, realTarget)) return { ok: false, code: 'escape', reason: '解析后的真实路径越出项目边界' };
151
+ if (!finalStat.isFile()) return { ok: false, code: 'not_file', reason: '目标不是普通文件' };
152
+ return { ok: true, absPath: path.join(baseDir, ...relPosix.split('/')), realPath: realTarget, stat: finalStat };
153
+ }
154
+ return res;
155
+ }
156
+ if (!res.stat.isFile()) return { ok: false, code: 'not_file', reason: '目标不是普通文件' };
157
+ if (!isInsideDir(realBaseDir, res.realPath)) return { ok: false, code: 'escape', reason: '解析后的真实路径越出项目边界' };
158
+ return { ok: true, absPath: res.absPath, realPath: res.realPath, stat: res.stat };
159
+ }
160
+
161
+ // 落点在文件系统里的真实相对路径:用 realpath(3) 取磁盘上的真实拼写(大小写、Unicode 规范化),
162
+ // 相对通道目录的真实路径;落点不是普通文件(不存在、符号链接、目录)或越出通道目录时返回 null。
163
+ // 大小写敏感的卷上,已存在的普通文件恒等于 relPosix。
164
+ export function resolveOnDiskCorePath(coreDir, relPosix) {
165
+ if (!validateRelativePosixPath(relPosix).ok) return null;
166
+ const absPath = path.join(coreDir, ...relPosix.split('/'));
167
+ let realCoreDir;
168
+ let real;
169
+ try {
170
+ if (!fs.lstatSync(absPath).isFile()) return null;
171
+ realCoreDir = fs.realpathSync.native(coreDir);
172
+ real = fs.realpathSync.native(absPath);
173
+ } catch {
174
+ return null;
175
+ }
176
+ const rel = path.relative(realCoreDir, real).split(path.sep).join('/');
177
+ return rel !== '' && !rel.startsWith('../') ? rel : null;
178
+ }
179
+
180
+ // lstat 已成功的目录项在磁盘上的真实拼写。符号链接不能用 realpath(会跟到目标),改用同目录里同 inode 的名字。
181
+ function trueLeafName(absPath, stat) {
182
+ if (!stat.isSymbolicLink()) return path.basename(fs.realpathSync.native(absPath));
183
+ const leaf = path.basename(absPath);
184
+ const dir = path.dirname(absPath);
185
+ const names = fs.readdirSync(dir);
186
+ if (names.includes(leaf)) return leaf;
187
+ for (const name of names) {
188
+ const other = fs.lstatSync(path.join(dir, name));
189
+ if (other.dev === stat.dev && other.ino === stat.ino) return name;
190
+ }
191
+ return leaf;
192
+ }
193
+
194
+ // 路径各段在通道目录里的实际情况,只用只读解析(lstat / realpath / readdir),不写任何东西:
195
+ // real 是已存在各段的真实拼写,kinds 是它们的类型(file | dir | link | other),
196
+ // missingFrom 是第一个还不存在的段的下标(等于段数表示整条路径都已存在)。
197
+ // 遇到符号链接段,或上级存在但不是目录,就停在那里并置 blocked:那不是「尚不存在」,不能再往下当成新路径。
198
+ export function traceCoreEntry(coreDir, relPosix) {
199
+ const segments = relPosix.split('/');
200
+ const real = [];
201
+ const kinds = [];
202
+ let abs = coreDir;
203
+ for (const segment of segments) {
204
+ abs = path.join(abs, segment);
205
+ let stat;
206
+ try {
207
+ stat = fs.lstatSync(abs);
208
+ } catch (err) {
209
+ if (!isMissingEntry(err)) throw err;
210
+ const blocked = kinds.length > 0 && kinds[kinds.length - 1] !== 'dir';
211
+ return { real, kinds, missingFrom: real.length, blocked };
212
+ }
213
+ real.push(trueLeafName(abs, stat));
214
+ kinds.push(stat.isSymbolicLink() ? 'link' : stat.isFile() ? 'file' : stat.isDirectory() ? 'dir' : 'other');
215
+ if (stat.isSymbolicLink()) return { real, kinds, missingFrom: real.length, blocked: true };
216
+ }
217
+ return { real, kinds, missingFrom: segments.length, blocked: false };
218
+ }
219
+
220
+ // 目录项身份:路径各段在磁盘上的真实拼写(相对通道目录);任一段还不存在时返回 null。
221
+ // 忽略大小写的卷上 Project.yaml 与 project.yaml 是同一个目录项,身份就是磁盘上的那一个拼写——
222
+ // 所以「是不是保护路径」要看身份,不能看请求时的拼写。大小写敏感的卷上身份恒等于 relPosix。
223
+ export function onDiskCorePath(coreDir, relPosix) {
224
+ if (!validateRelativePosixPath(relPosix).ok) return null;
225
+ const trace = traceCoreEntry(coreDir, relPosix);
226
+ return trace.missingFrom === relPosix.split('/').length ? trace.real.join('/') : null;
227
+ }
228
+
229
+ // 保护命名空间的实际身份:各顶层保护名在磁盘上的真实拼写;这个名字还没有目录项时就是它本身。
230
+ // 名单来自 NON_UPSTREAM_TOP_FILES / NON_UPSTREAM_TOP_DIRS,别的模块加进去的名字自动生效。
231
+ export function protectedCoreIdentities(coreDir) {
232
+ return mapProtectedCoreIdentities(name => onDiskCorePath(coreDir, name));
233
+ }
234
+
235
+ export function mapProtectedCoreIdentities(identify) {
236
+ const files = new Set();
237
+ const dirs = new Set();
238
+ for (const name of NON_UPSTREAM_TOP_FILES) files.add(identify(name) ?? name);
239
+ for (const name of NON_UPSTREAM_TOP_DIRS) dirs.add(identify(name) ?? name);
240
+ return { files, dirs };
241
+ }
242
+
243
+ // 一个目录项身份是否落在保护命名空间里:是某个受保护的顶层文件,或在某个受保护的目录之下。
244
+ export function isProtectedIdentity(realRel, identities) {
245
+ if (identities.files.has(realRel)) return true;
246
+ for (const dir of identities.dirs) {
247
+ if (realRel === dir || realRel.startsWith(`${dir}/`)) return true;
248
+ }
249
+ return false;
250
+ }
251
+
252
+ // 写命令专用:给发行路径在目标卷上定「目录项身份」,并报出所有必须在写入之前拒绝的情况。
253
+ // 已存在的条目按磁盘上的真实拼写认;尚不存在的名字与谁等价,不做 JS 折叠,也不假设卷折不折叠,
254
+ // 而是在通道目录所在卷独占建一个临时命名空间,把保护名和发行路径按原拼写独占创建,让文件系统自己回答——
255
+ // 独占创建撞上(EEXIST)就是同一个目录项。临时命名空间只在有新路径时才建,用完即删。
256
+ // 返回 { entries: Map<发行路径, 真实身份|null>, errors: string[] };errors 非空时调用方必须拒绝写入。
257
+ export function planReleaseIdentities(coreDir, relPaths, { coreDirName = '.aodw-next' } = {}) {
258
+ const protectedNs = protectedCoreIdentities(coreDir);
259
+ const entries = new Map();
260
+ const errors = [];
261
+ const owners = new Map(); // 现存目录项的身份 -> 发行路径
262
+ const fresh = []; // 还没有完整目录项的发行路径
263
+ for (const rel of relPaths) {
264
+ if (!validateRelativePosixPath(rel).ok) {
265
+ errors.push(`发行路径非法 (${coreDirName}/${rel})`);
266
+ continue;
267
+ }
268
+ const trace = traceCoreEntry(coreDir, rel);
269
+ const identity = trace.real.join('/');
270
+ if (trace.blocked) {
271
+ errors.push(`上游落点路径异常 (${coreDirName}/${rel}): 路径上有符号链接,或上级不是目录`);
272
+ } else if (trace.missingFrom < rel.split('/').length) {
273
+ if (isProtectedIdentity(identity, protectedNs)) errors.push(`上游落点 ${coreDirName}/${rel} 指向受保护路径 (${identity}/…)`);
274
+ else fresh.push({ rel, parent: identity, remainder: rel.split('/').slice(trace.missingFrom) });
275
+ } else if (trace.kinds[trace.kinds.length - 1] !== 'file') {
276
+ errors.push(`上游落点路径异常 (${coreDirName}/${rel}): 不是普通文件`);
277
+ } else if (isProtectedIdentity(identity, protectedNs)) {
278
+ errors.push(`上游落点 ${coreDirName}/${rel} 指向受保护路径 (${identity})`);
279
+ } else if (owners.has(identity)) {
280
+ errors.push(`发行路径 ${coreDirName}/${rel} 与 ${coreDirName}/${owners.get(identity)} 是同一个目录项 (${identity})`);
281
+ } else {
282
+ owners.set(identity, rel);
283
+ entries.set(rel, identity);
284
+ }
285
+ }
286
+ if (fresh.length > 0) {
287
+ let held = null;
288
+ try {
289
+ held = fs.mkdtempSync(path.join(coreDir, '.aodw-plan-'));
290
+ } catch (err) {
291
+ // 通道目录不可写时建不了临时命名空间,新路径在目标卷上的身份就判断不了:如实拒绝,不猜。
292
+ errors.push(`无法在 ${coreDirName}/ 内建立临时命名空间,判断不了新增路径在目标卷上的身份: ${err.message}`);
293
+ }
294
+ if (held !== null) {
295
+ try {
296
+ askFileSystem(held, fresh, coreDirName, errors);
297
+ } finally {
298
+ try {
299
+ fs.rmSync(held, { recursive: true, force: true });
300
+ } catch (err) {
301
+ errors.push(`临时命名空间 ${coreDirName}/${path.basename(held)} 没能删除,请手工删除: ${err.message}`);
302
+ }
303
+ }
304
+ }
305
+ }
306
+ for (const { rel } of fresh) entries.set(rel, null);
307
+ return { entries, errors };
308
+ }
309
+
310
+ // 临时命名空间 held 里只放本轮独占建的组容器(mkdtemp,随机名):每个「已有的上级目录」各对应一个,根级也有一个。
311
+ // 保护名的占位放在根组容器里;发行名字只建在各自组的容器里面。发行名字与辅助用的容器名永远不同层,
312
+ // 所以不需要回避或保留任何内部名字。文件系统仍然只回答一件事:按原拼写独占创建会不会撞上。
313
+ function askFileSystem(held, fresh, coreDirName, errors) {
314
+ const holders = new Map(); // 临时命名空间里的目录项 -> { rel } 或 { protected: 名字 }
315
+ const create = (dir, name, holder, directory) => {
316
+ const abs = path.join(dir, name);
317
+ try {
318
+ if (directory) fs.mkdirSync(abs);
319
+ else fs.closeSync(fs.openSync(abs, 'wx', 0o600));
320
+ } catch (err) {
321
+ if (err.code !== 'EEXIST') throw err;
322
+ return null; // 撞上了:这个名字在目标卷上已经有目录项
323
+ }
324
+ holders.set(fs.realpathSync.native(abs), holder);
325
+ return abs;
326
+ };
327
+ const groups = new Map(); // 已有的上级目录的身份 -> 本轮独占建的、代表它的组容器
328
+ const groupContainer = (parent) => {
329
+ if (!groups.has(parent)) groups.set(parent, fs.mkdtempSync(path.join(held, 'g-')));
330
+ return groups.get(parent);
331
+ };
332
+ if (fresh.some((item) => item.parent === '')) {
333
+ const root = groupContainer('');
334
+ for (const name of NON_UPSTREAM_TOP_FILES) create(root, name, { protected: name }, false);
335
+ for (const name of NON_UPSTREAM_TOP_DIRS) create(root, name, { protected: name }, true);
336
+ }
337
+ for (const { rel, parent, remainder } of fresh) {
338
+ let current = groupContainer(parent);
339
+ for (let i = 0; i < remainder.length; i += 1) {
340
+ const name = remainder[i];
341
+ const last = i === remainder.length - 1;
342
+ const created = create(current, name, { rel }, !last);
343
+ if (created !== null) {
344
+ current = created;
345
+ continue;
346
+ }
347
+ // 问文件系统:这个名字实际落到哪个目录项上。
348
+ const actual = fs.realpathSync.native(path.join(current, name));
349
+ const holder = holders.get(actual);
350
+ if (holder === undefined) throw new Error(`临时命名空间里出现不属于本次的目录项: ${actual}`);
351
+ const isDir = fs.lstatSync(actual).isDirectory();
352
+ if (holder.protected !== undefined) {
353
+ errors.push(`上游落点 ${coreDirName}/${rel} 指向受保护路径 (${holder.protected}${isDir ? '/…' : ''})`);
354
+ } else if (isDir && !last) {
355
+ current = actual; // 与另一条发行路径共用同一个上级目录,继续往下
356
+ } else if (isDir === last) {
357
+ errors.push(`发行路径 ${coreDirName}/${rel} 与 ${coreDirName}/${holder.rel} 在目标卷上需要同一个目录项既是文件又是目录`);
358
+ } else {
359
+ errors.push(`发行路径 ${coreDirName}/${rel} 与 ${coreDirName}/${holder.rel} 在目标卷上落到同一个目录项`);
360
+ }
361
+ break;
362
+ }
363
+ }
364
+ }
365
+
366
+ // 当前卷是否忽略文件名大小写:在 dir 内独占建一个临时目录,用只差大小写的名字探一次,随后删除,不留任何文件。
367
+ // 只在发行路径之间出现只差大小写或 Unicode 规范化的名字时才需要调用;大小写敏感的卷上调用方不必改变行为。
368
+ export function volumeIgnoresCase(dir) {
369
+ const probeDir = fs.mkdtempSync(path.join(dir, '.aodw-case-probe-'));
370
+ try {
371
+ fs.writeFileSync(path.join(probeDir, 'probe'), '');
372
+ return fs.existsSync(path.join(probeDir, 'PROBE'));
373
+ } finally {
374
+ fs.rmSync(probeDir, { recursive: true, force: true });
375
+ }
376
+ }
377
+
378
+ // 替换文件目录项而不改旧 inode。整个函数只有一个提交点:最后一步 rename(temp, absPath),新内容在那一刻
379
+ // 对外生效;临时文件用 wx 独占创建(写入期间 0600,不被 umask 放宽),写完改成指定权限。
380
+ // 目标已有、且是拼写(大小写、Unicode 规范化)与请求不同的普通文件时,提交前先把这一条目录项改成目标拼写,
381
+ // 内容不动——大小写不敏感的卷上 rename 覆盖不会改拼写。卷不肯改(例如只差 Unicode 规范化)时拼写保持原样,
382
+ // 内容照常提交,由返回值的 spelling 如实说明。目标若与别的名字是硬链接,那个名字保持原字节与权限。
383
+ // 口径:抛错 ⇔ 新内容没有提交(旧内容仍在,只清本次的临时文件);返回 ⇔ 已提交。
384
+ // 调用方须先确认 absPath 的上级目录段都不是符号链接(assertSafeWriteTargets)。
385
+ export function replaceFileEntry(absPath, data, mode) {
386
+ // 字符串先转成 Buffer:内容或权限不合法时在创建临时文件之前就抛错。
387
+ const content = Buffer.from(data);
388
+ if (!Number.isInteger(mode)) throw new TypeError('mode 必须是整数权限位');
389
+ const leaf = path.basename(absPath);
390
+ let existingLeaf = null;
391
+ try {
392
+ if (fs.lstatSync(absPath).isFile()) existingLeaf = path.basename(fs.realpathSync.native(absPath));
393
+ } catch (err) {
394
+ if (err.code !== 'ENOENT') throw err;
395
+ }
396
+ const temp = `${absPath}.aodw-tmp-${process.pid}`;
397
+ // 失败(如已有同名临时文件)时 temp 不是本次建的,不动它。
398
+ let fd = fs.openSync(temp, 'wx', 0o600);
399
+ try {
400
+ let written = 0;
401
+ while (written < content.length) written += fs.writeSync(fd, content, written);
402
+ fs.fchmodSync(fd, mode & 0o777);
403
+ fs.closeSync(fd);
404
+ fd = undefined;
405
+ // 只改旧条目的拼写。这一步或下一步失败时旧内容都还在,新内容没有提交。
406
+ if (existingLeaf !== null && existingLeaf !== leaf) {
407
+ fs.renameSync(path.join(path.dirname(absPath), existingLeaf), absPath);
408
+ }
409
+ // 提交点。
410
+ fs.renameSync(temp, absPath);
411
+ } catch (err) {
412
+ if (fd !== undefined) {
413
+ try { fs.closeSync(fd); } catch { /* 关闭失败不掩盖原始错误 */ }
414
+ }
415
+ try { fs.unlinkSync(temp); } catch { /* 临时文件已不在就无需清理 */ }
416
+ err.aodwWrite = { target: absPath, committed: false };
417
+ throw err;
418
+ }
419
+ let spelling = leaf;
420
+ try {
421
+ spelling = path.basename(fs.realpathSync.native(absPath));
422
+ } catch { /* 取不到真实拼写就按请求拼写计 */ }
423
+ return {
424
+ committed: true,
425
+ replaced: existingLeaf !== null,
426
+ renamed: existingLeaf !== null && existingLeaf !== leaf,
427
+ spelling,
428
+ };
429
+ }
430
+
431
+ // 写前防护:这些仓内相对路径上任何已存在的段都不是符号链接,写入才不会落到仓库之外。
432
+ export function assertSafeWriteTargets(projectRoot, relPosixPaths) {
433
+ const symlinks = new Set();
434
+ const invalid = [];
435
+ for (const rel of relPosixPaths) {
436
+ const res = walkRealPath(projectRoot, rel, { allowMissing: true });
437
+ if (res.ok) continue;
438
+ if (res.code === 'symlink') {
439
+ symlinks.add(path.relative(projectRoot, res.absPath).split(path.sep).join('/'));
440
+ } else {
441
+ invalid.push(`${rel}: ${res.reason}`);
442
+ }
443
+ }
444
+ const found = [...symlinks].sort();
445
+ const errors = found
446
+ .map((p) => `写入路径包含符号链接,拒绝改写其目标: ${p}`)
447
+ .concat(invalid.sort());
448
+ return { ok: errors.length === 0, errors, symlinks: found };
449
+ }
@@ -0,0 +1,36 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { collectUpstreamFiles } from './lock.js';
4
+
5
+ export const ORCA_RULE = '02-workflow/orca-multi-session.md';
6
+ export const ORCA_GUIDE = '06-project/dev-session-guide.md';
7
+ // 只在 dev_env=orca 时安装的上游文件;06-project/dev-session-guide.md 属于项目层,由 init 另行生成。
8
+ export const ORCA_UPSTREAM_FILES = [
9
+ ORCA_RULE,
10
+ 'templates/orca-handoff-template.md',
11
+ 'templates/dev-session-guide-template.md',
12
+ 'templates/main-session-handover-template.md'
13
+ ];
14
+
15
+ // dev_env=none 时不安装的 Orca 文件(绝对路径),init 的整树复制与上游清单共用。
16
+ export function orcaExclusions(sourceCore, devEnv) {
17
+ return new Set(devEnv === 'orca' ? [] : ORCA_UPSTREAM_FILES.map(file => path.join(sourceCore, file)));
18
+ }
19
+
20
+ // none 安装的 manifest.yaml 去掉 Orca 模块登记段,其余字节与注释保持不变。
21
+ export function stripOrcaManifestSection(manifest) {
22
+ return manifest.replace(/^ - id: orca-multi-session\n(?: .+\n)+\n/m, '');
23
+ }
24
+
25
+ // 当前 dev_env 下实际安装的上游文件(相对 .aodw-next/ 的路径,码点序)。
26
+ // 项目层与运行态配置由锁的所有权边界排除,不在此列。
27
+ export function collectReleaseFiles(sourceCore, devEnv) {
28
+ return collectUpstreamFiles(sourceCore, orcaExclusions(sourceCore, devEnv));
29
+ }
30
+
31
+ // 发行文件落盘后的字节:只有 manifest.yaml 在 none 下去掉 Orca 登记段,其余与发行模板相同。
32
+ export function readReleaseFile(sourceCore, relPath, devEnv) {
33
+ const content = fs.readFileSync(path.join(sourceCore, ...relPath.split('/')));
34
+ if (relPath !== 'manifest.yaml' || devEnv === 'orca') return content;
35
+ return Buffer.from(stripOrcaManifestSection(content.toString('utf8')), 'utf8');
36
+ }