baodan/.agents/skills/impeccable/scripts/live/source-search.mjs
wsb1224 e3479f0546 上线阻断问题全部修复
#	问题	修复	文件
1	前端构建失败(引号错误)	size="small type=" → size="small" type="	PosterHistoryPage.vue
2	migrate_014 ORM vs 缺失列	全部改为原始 SQL,不再引用 ORM 模型	migrate_014.py
3	cleanup 字段名错误	output_path → ppt_path	cleanup.py
4	文案生成 case 越权	添加 case.user_id != user_id 校验	poster/service.py
5	存储路径未接通持久化卷	全部改用 get_storage_root()(默认 /app/api/storage/insurance)	config.py, ppt/routes.py, poster/service.py, poster/tasks.py
高风险问题修复
#	问题	修复	文件
6	migrate_019 rollback 撤销成功字段	每个 ALTER 后立即 commit,失败只回滚当前语句	migrate_019.py
7	迁移锁 Windows 不兼容 + 句柄未持久化	全局变量保存锁句柄,支持 Windows msvcrt	api/insurance/db/__init__.py
8	PDF 校验异常时放行	异常返回 False(文件损坏)	security.py
9	健康检查始终返回成功	缺少关键资源时返回 503 + missing 列表	poster/routes.py
10	短密钥掩码泄露原值	≤4 字符返回 ****	ppt_admin_service.py
11	设置无键名白名单	添加 _ALLOWED_SETTING_KEYS 白名单	ppt_admin_service.py
12	容器重启任务永久 stuck	添加 recover_stale_tasks() 启动恢复函数	poster/tasks.py, ppt/parse_worker.py
2026-07-27 13:52:09 +08:00

106 lines
4.2 KiB
JavaScript

/**
* The project-source walk shared by live-wrap.mjs and live-accept.mjs.
*
* Both scripts need the same thing: find the one project file containing a
* string (wrap looks for the element's class/id/text, accept looks for the
* session's `impeccable-variants-start` marker). They had two near-identical
* copies of the walk, and the copies drifted — same `EXTENSIONS` array declared
* twice, same `searchDirs` array declared twice, one `realpathSync` guarded by
* try/catch and the other not. That drift is what #374 had to patch in two
* places at once.
*
* Callers differ only in how they reject a candidate, so that is the one thing
* this module takes as options (`skipDirs`, `fileFilter`).
*/
import fs from 'node:fs';
import path from 'node:path';
import { IMPECCABLE_DIR } from '../lib/impeccable-paths.mjs';
import { matchesTemplateExtension } from '../lib/template-extensions.mjs';
/**
* Privileged roots, searched in order, before the catch-all `.` walk.
*
* `lib` is here for Phoenix, whose templates live in `lib/my_app_web/`. It is
* an ordering preference rather than a reachability fix: `.` already recurses
* into `lib`, so the real #374 bug was the extension list, not this array.
*/
export const SOURCE_SEARCH_DIRS = Object.freeze([
'src', 'app', 'pages', 'components', 'public', 'views', 'templates', 'lib', '.',
]);
/**
* Directories that are never project source.
*
* `.impeccable` is the critical entry, and it is not cosmetic. Progressive
* publication stages each revision as `.impeccable/live/artifacts/
* <id>-r<n>.<source-ext>`, and those artifacts carry the very marker accept
* searches for. The walk reaches `.` for any project whose source is not under
* one of the privileged roots above (this repo's own site lives in
* `site/pages/`), and dot-directories sort before letters, so the artifact was
* found *before* the real file. isGeneratedFile then declined the accept, and
* the agent fell back to carbonizing several hundred lines of stylesheet by
* hand.
*/
export const NEVER_SOURCE_DIRS = Object.freeze(['node_modules', '.git', IMPECCABLE_DIR]);
const MAX_DEPTH = 5;
/**
* Walk the project for the first template file whose contents include `query`.
*
* @param {object} opts
* @param {string} opts.query substring to find in file contents
* @param {string} opts.cwd project root
* @param {string[]} opts.extensions filename suffixes that count as templates
* @param {Iterable<string>} [opts.skipDirs] directory names never to descend into
* @param {(filePath: string) => boolean} [opts.fileFilter] return false to reject a candidate
* @returns {string|null} absolute path of the first match
*/
export function findSourceFile({ query, cwd, extensions, skipDirs = NEVER_SOURCE_DIRS, fileFilter }) {
const skip = new Set(skipDirs);
const seen = new Set();
for (const dir of SOURCE_SEARCH_DIRS) {
const absDir = path.join(cwd, dir);
if (!fs.existsSync(absDir)) continue;
const result = walk(absDir, query, extensions, skip, fileFilter, seen, 0);
if (result) return result;
}
return null;
}
function walk(dir, query, extensions, skip, fileFilter, seen, depth) {
if (depth > MAX_DEPTH) return null;
// A broken symlink anywhere in the tree used to throw straight out of
// live-wrap's copy of this walk, killing the whole wrap.
let realDir;
try { realDir = fs.realpathSync(dir); } catch { return null; }
if (seen.has(realDir)) return null;
seen.add(realDir);
let entries;
try { entries = fs.readdirSync(dir, { withFileTypes: true }); }
catch { return null; }
// Files before directories: a match in the current directory beats one
// nested deeper.
for (const entry of entries) {
if (!entry.isFile()) continue;
if (!matchesTemplateExtension(entry.name, extensions)) continue;
const filePath = path.join(dir, entry.name);
if (fileFilter && !fileFilter(filePath)) continue;
try {
if (fs.readFileSync(filePath, 'utf-8').includes(query)) return filePath;
} catch { /* unreadable, skip */ }
}
for (const entry of entries) {
if (!entry.isDirectory()) continue;
if (skip.has(entry.name)) continue;
const result = walk(path.join(dir, entry.name), query, extensions, skip, fileFilter, seen, depth + 1);
if (result) return result;
}
return null;
}