From be126525c5252d9113610ebf55564c6a823ac8bd Mon Sep 17 00:00:00 2001 From: Agent Workbench Dev Date: Sun, 1 Nov 2026 02:29:58 +0800 Subject: [PATCH] feat(context): persist bounded source-verified parsing cache --- docs/QA-1.19.md | 12 +- docs/RETROSPECTIVE-1.19.md | 4 + .../first-failure.tap | 35 ++++ docs/project-cache-bc-evidence/fixed.tap | 34 ++++ progress.md | 2 + src/core/context/index-cache.mjs | 116 +++++++++++++ src/core/context/project-index.mjs | 83 +++++++-- src/core/project-inspector.mjs | 14 +- src/server/runtime.mjs | 2 +- task_plan.md | 6 +- tests/project-cache.test.mjs | 163 ++++++++++++++++++ 11 files changed, 451 insertions(+), 20 deletions(-) create mode 100644 docs/project-cache-bc-evidence/first-failure.tap create mode 100644 docs/project-cache-bc-evidence/fixed.tap create mode 100644 src/core/context/index-cache.mjs create mode 100644 tests/project-cache.test.mjs diff --git a/docs/QA-1.19.md b/docs/QA-1.19.md index 4e5c5f0f..98ee0c3b 100644 --- a/docs/QA-1.19.md +++ b/docs/QA-1.19.md @@ -5,8 +5,8 @@ | 切片 | 状态 | 原证据/结论 | | --- | --- | --- | | A 测量 | 已完成 | project-cache-a-evidence/baseline.json;3种样本各冷/热3轮/新实例/同大小mtime修改,共18次 | -| B 有界缓存 | 待开发 | 以A测量为依据 | -| C 严格失效 | 待开发 | 当前来源核对不可省略 | +| B 有界缓存 | 已实现/专项通过 | 8项目、内存/磁盘各32MiB、单项目8MiB;目录/文件/结构/摘要核对,失败回退 | +| C 严格失效 | 已实现/专项通过 | 22/22含旧索引;配置/分支、同大小mtime、权限/链接、不完整扫描、并发变化及清除竞态 | | D 产品入口 | 待开发 | 工程索引/诊断、桌面/390px | | E 同题工程 | 待验收 | Node/React/Python、检查/页面/Git | | F 完整发行 | 待验收 | 独立源码、完整UI、便携/安装/升级、原生回看 | @@ -18,3 +18,11 @@ QA使用独立目录和端口。本地解析命中、上游prompt缓存、实际 固定Node24.22.1;每种工程72份源码、每份36个声明,附一份包/工程声明。原输入SHA及同大小mtime变更前后SHA分别保存。读计数仅统计所选工程源码/声明,不含派生缓存读写。Node冷148.7ms、热中位84.0ms、新实例169.0ms;React冷315.3ms、热中位75.1ms、新实例189.6ms;Python冷100.7ms、热中位74.6ms、新实例98.0ms。首次解析读取146/146/147次,热74/74/75次;同大小mtime变化只更新1份,来源指纹变化。单次耗时受Git/调度影响,不宣称全任务倍数收益。 现有缓存对同进程足够;跨实例仍全部重解析,AST样本存在可复用收益。B仅增加有界持久解析材料,不缓存请求/工具副作用或跳过源码SHA。Python收益较小,后续比较照实记录。项目记忆已有来源/过期核对;请求验证缓存局限原次验证,不扩为跨请求权限依据。审查发现旧内存复用只绑定文件SHA,缺少解析配置、Git分支和不完整扫描的显式隔离;C补齐来源闭合检查及相关测试。 + +## B/C 实现与专项 + +单一ProjectIndex复用流程增加持久派生材料,绑定projectWorkspaceIdentity、解析器/格式修订/预算及Git HEAD/branch;逐文件核对本次SHA、字节数、文件访问身份。当前源码扫描不完整时不用旧材料且不保存新缓存。建立后再做一次有界完整快照,源码、路径集、访问范围和分支变化以409拒绝;清除/关闭期间的pending不能重新填入缓存。 + +仅保存声明/导入和派生签名,没有源码正文、模型Key、请求、审批或工具结果。内存与磁盘各最多8项目/32MiB,单项目8MiB;超限仍返回当前索引。损坏摘要/JSON/字段重建,外链目录、软/硬链接缓存文件拒绝。settings仅保存enabled;清除仅删除当前项目摘要命名的派生文件。原工件和冻结请求不改写。 + +新增11项加原11项共22/22通过,原日志project-cache-bc-evidence/fixed.tap。第一次8/9暴露损坏symbols含null的验证器异常,修正结构检查后重建通过,失败原件first-failure.tap保留。实际chmod只证明文件mode变化;拒绝读取通过EACCES注入验证,不把注入称为Windows ACL实机。性能与完整工程结果待E。 diff --git a/docs/RETROSPECTIVE-1.19.md b/docs/RETROSPECTIVE-1.19.md index 7b39b55d..f1911bc5 100644 --- a/docs/RETROSPECTIVE-1.19.md +++ b/docs/RETROSPECTIVE-1.19.md @@ -9,3 +9,7 @@ ## A 回顾 18次固定样本验证现有热解析复用已有效,跨实例Node/React仍需重解析,Python只有较小空间。选择持久化有界派生声明/导入材料;不再做另一个项目记忆或工件缓存。测量同时保存输入原SHA和读次数,保留波动较大的单轮数据,后续记录来源闭合检查的新增开销,不能只挑最快数字。下一步B/C共同完善身份/失效,再单独验收产品入口。 + +## B/C 回顾 + +补齐缓存身份和来源闭合,而非只把旧Map写到磁盘。两轮SHA读取增加成本,E必须与原基线一起报告。首轮损坏结构验证失败已修正并保留原失败,后续结构字段先验证再使用。22/22通过后进入D,把当前核对、上次检索和派生存储分清,清除不应假装执行了新检索。来源链接测试和缓存目录链接拒绝分开证明,实际权限mode与注入EACCES明确标注。 diff --git a/docs/project-cache-bc-evidence/first-failure.tap b/docs/project-cache-bc-evidence/first-failure.tap new file mode 100644 index 00000000..91e6b1cc --- /dev/null +++ b/docs/project-cache-bc-evidence/first-failure.tap @@ -0,0 +1,35 @@ +✔ persistent parsing is project/SHA/config bound and produces identical semantic indexes after restart (1703.0704ms) +warning: in the working copy of 'README.md', LF will be replaced by CRLF the next time Git touches it +warning: in the working copy of 'main.ts', LF will be replaced by CRLF the next time Git touches it +warning: in the working copy of 'value.ts', LF will be replaced by CRLF the next time Git touches it +✔ same-size restored-mtime changes, additions, renames and deletions cannot reuse stale entries or import targets (1320.9107ms) +Switched to a new branch 'other' +✔ branch and commit changes invalidate even byte-identical sources (2194.8297ms) +✖ corrupt, malformed and schema-invalid records fall back to fresh parsing (852.4814ms) +✔ disable and clear persist without touching sources, sessions, configuration or original artifacts (1046.7693ms) +✔ cache capacity bounds disk and memory by bytes and LRU project count (1361.0293ms) +✔ incomplete scans do not load or save cached entries; unavailable files leave the current result scope (939.952ms) +✔ project and cache junctions cannot expose or delete an outside directory (907.6364ms) +✔ concurrent file changes refuse a snapshot and clear during pending work cannot repopulate it (868.5737ms) +ℹ tests 9 +ℹ suites 0 +ℹ pass 8 +ℹ fail 1 +ℹ cancelled 0 +ℹ skipped 0 +ℹ todo 0 +ℹ duration_ms 11294.0343 + +✖ failing tests: + +test at tests\project-cache.test.mjs:67:1 +✖ corrupt, malformed and schema-invalid records fall back to fresh parsing (852.4814ms) + TypeError [Error]: Cannot read properties of null (reading 'name') + at file:///D:/Ai-project/REA%E9%80%86%E5%90%91/Agent-Workbench/src/core/context/project-index.mjs:255:53 + at Array.every () + at ProjectIndex.validEntry (file:///D:/Ai-project/REA%E9%80%86%E5%90%91/Agent-Workbench/src/core/context/project-index.mjs:255:24) + at ProjectIndex.build (file:///D:/Ai-project/REA%E9%80%86%E5%90%91/Agent-Workbench/src/core/context/project-index.mjs:182:111) + at process.processTicksAndRejections (node:internal/process/task_queues:104:5) + at async TestContext. (file:///D:/Ai-project/REA%E9%80%86%E5%90%91/Agent-Workbench/tests/project-cache.test.mjs:73:21) + at async Test.run (node:internal/test_runner/test:1409:7) + at async Test.processPendingSubtests (node:internal/test_runner/test:974:7) diff --git a/docs/project-cache-bc-evidence/fixed.tap b/docs/project-cache-bc-evidence/fixed.tap new file mode 100644 index 00000000..55636d96 --- /dev/null +++ b/docs/project-cache-bc-evidence/fixed.tap @@ -0,0 +1,34 @@ +✔ persistent parsing is project/SHA/config bound and produces identical semantic indexes after restart (1828.6494ms) +warning: in the working copy of 'README.md', LF will be replaced by CRLF the next time Git touches it +warning: in the working copy of 'main.ts', LF will be replaced by CRLF the next time Git touches it +warning: in the working copy of 'value.ts', LF will be replaced by CRLF the next time Git touches it +✔ same-size restored-mtime changes, additions, renames and deletions cannot reuse stale entries or import targets (1768.327ms) +Switched to a new branch 'other' +✔ branch and commit changes invalidate even byte-identical sources (2756.5008ms) +✔ corrupt, malformed and schema-invalid records fall back to fresh parsing (1104.8019ms) +✔ disable and clear persist without touching sources, sessions, configuration or original artifacts (1066.292ms) +✔ cache capacity bounds disk and memory by bytes and LRU project count (1757.6078ms) +✔ incomplete scans do not load or save cached entries; unavailable files leave the current result scope (1337.1784ms) +✔ project and cache junctions cannot expose or delete an outside directory (1069.2365ms) +✔ concurrent file changes refuse a snapshot and clear during pending work cannot repopulate it (684.3553ms) +✔ file access mode changes invalidate parsed material even when bytes are unchanged (824.2543ms) +✔ tracked directory replaced by a junction invalidates access scope and never uses its old cache (1704.2802ms) +✔ JS/TS/React AST indexes actual declarations, exports, imports and verified source lines (1098.8196ms) +✔ default expressions, named default exports and declared functions retain their AST evidence (665.0237ms) +✔ incremental refresh invalidates changed/deleted symbols and recalculates reused import targets (1685.6009ms) +✔ Python candidates exclude comments and multiline strings and declare limited evidence (983.3435ms) +✔ indexes isolate project identity and root and evict bounded caches without sharing results (1204.8367ms) +✔ Git ignore, credential paths and links stay outside the index and no project files are executed (1278.8008ms) +✔ malformed, binary, non-UTF8 and bounded files expose partial coverage while good files remain searchable (989.8401ms) +✔ index query rejects invalid limits/types and does not imply reference or alias resolution (655.0078ms) +✔ read-only engineering inspection disables configured Git clean filters and preserves index bytes (2083.9645ms) +✔ Python multiline declarations retain candidate scope without interpreting expressions as declarations (1131.2671ms) +✔ TypeScript module candidates support import-equals and MTS/CTS while opaque imports stay explicit (1157.7477ms) +ℹ tests 22 +ℹ suites 0 +ℹ pass 22 +ℹ fail 0 +ℹ cancelled 0 +ℹ skipped 0 +ℹ todo 0 +ℹ duration_ms 15998.1911 diff --git a/progress.md b/progress.md index 1ab21549..b0c9dbf6 100644 --- a/progress.md +++ b/progress.md @@ -1,5 +1,7 @@ # 统一执行进度 +1.19 B/C首轮专项8/9:损坏缓存内symbols含null时验证器抛异常,未走重建。原失败保留.qa/project-cache-bc-first.tap;修正为完整元素结构核对,并补充实际只读mode变化及Git已跟踪目录被junction替换测试。旧索引11/11通过;正式服务未修改。 + 1.19 A已完成18次固定样本测量,原输入SHA/变更SHA/逐轮耗时/读次数存档。Node/React跨实例重解析有实际空间,Python收益较小;不折算token。接下来B/C实现单一有界持久解析缓存并完善分支/配置/不完整扫描与并发来源核对。首次批量只读输出过大截断,后续限定短段读取,无文件或服务副作用。 1.19开始:已恢复1.18最终交付与PLAN-1.19,读取仓库规范和规划/产品UI/前端技能。起点HEAD606b5ec、工作区干净,建立feat/project-cache-1.19。当前仅开始A测量与范围审查,未改正式服务、未启动真实模型请求;后续每切片回顾、检查并本地提交。 diff --git a/src/core/context/index-cache.mjs b/src/core/context/index-cache.mjs new file mode 100644 index 00000000..548d8595 --- /dev/null +++ b/src/core/context/index-cache.mjs @@ -0,0 +1,116 @@ +import fs from 'node:fs'; +import path from 'node:path'; +import { createHash, randomUUID } from 'node:crypto'; + +const hash = value => createHash('sha256').update(value).digest('hex'); +const samePath = (a, b) => process.platform === 'win32' ? a.toLowerCase() === b.toLowerCase() : a === b; +const recordName = /^[a-f0-9]{64}\.json$/; +const failure = () => new Error('派生缓存目录或文件无法核对,已回退当前源码重建。'); + +// Stores only derived declarations/imports. Never stores source bodies, requests, +// credentials, execution results or recovery state. Every hit needs a fresh SHA. +export class IndexCache { + constructor({ directory, maximumProjects = 8, maximumBytes = 32 * 1024 * 1024, maximumEntryBytes = 8 * 1024 * 1024 } = {}) { + this.directory = directory ? path.resolve(directory) : null; + this.maximumProjects = maximumProjects; this.maximumBytes = maximumBytes; this.maximumEntryBytes = maximumEntryBytes; + this.enabled = true; this.error = null; + if (this.directory) { + try { + this.assertDirectory(); + const filename = path.join(this.directory, 'settings.json'); + if (fs.existsSync(filename)) { + this.assertFile(filename, 1024); + const settings = JSON.parse(fs.readFileSync(filename, 'utf8')); + if (settings.schemaVersion !== 1 || typeof settings.enabled !== 'boolean') throw failure(); + this.enabled = settings.enabled; + } + } catch { this.enabled = false; this.error = '缓存设置无法核对,缓存已关闭;可修复目录后重新启用。'; } + } + } + key(project) { return hash(project.id + '\0' + project.root); } + assertDirectory() { + fs.mkdirSync(this.directory, { recursive: true, mode: 0o700 }); + const stat = fs.lstatSync(this.directory); + if (!stat.isDirectory() || stat.isSymbolicLink() || !samePath(fs.realpathSync.native(this.directory), this.directory)) throw failure(); + } + assertFile(filename, maximum = this.maximumEntryBytes) { + const stat = fs.lstatSync(filename); + if (!stat.isFile() || stat.isSymbolicLink() || stat.nlink !== 1 || stat.size > maximum) throw failure(); + return stat; + } + write(filename, value) { + this.assertDirectory(); + if (fs.existsSync(filename)) this.assertFile(filename, Math.max(1024, this.maximumEntryBytes)); + const temporary = path.join(this.directory, '.write-' + randomUUID() + '.tmp'); + try { + fs.writeFileSync(temporary, value, { flag: 'wx', mode: 0o600 }); + this.assertDirectory(); + if (fs.existsSync(filename)) this.assertFile(filename, Math.max(1024, this.maximumEntryBytes)); + fs.renameSync(temporary, filename); + } finally { if (fs.existsSync(temporary)) fs.unlinkSync(temporary); } + } + load(project, identity) { + if (!this.enabled || !this.directory) return { entries: null, reason: this.enabled ? 'process-only' : 'disabled' }; + try { + this.assertDirectory(); + const filename = path.join(this.directory, this.key(project) + '.json'); + if (!fs.existsSync(filename)) return { entries: null, reason: 'cold' }; + this.assertFile(filename); + const envelope = JSON.parse(fs.readFileSync(filename, 'utf8')); + if (envelope.schemaVersion !== 1 || hash(JSON.stringify(envelope.payload)) !== envelope.sha256) return { entries: null, reason: 'corrupt' }; + const payload = envelope.payload; + if (JSON.stringify(payload.identity) !== JSON.stringify(identity)) return { entries: null, reason: 'identity-changed' }; + if (!Array.isArray(payload.entries) || payload.entries.length > 1000) return { entries: null, reason: 'corrupt' }; + return { entries: new Map(payload.entries.map(entry => [entry.path, entry])), reason: 'disk' }; + } catch { this.error = '派生缓存读取失败;本次从当前源码重建。'; return { entries: null, reason: 'unavailable' }; } + } + save(project, identity, entries) { + if (!this.enabled || !this.directory) return { stored: false, reason: this.enabled ? 'process-only' : 'disabled' }; + try { + const payload = { identity, entries: [...entries.values()] }; + const text = JSON.stringify({ schemaVersion: 1, sha256: hash(JSON.stringify(payload)), payload }); + if (Buffer.byteLength(text) > Math.min(this.maximumEntryBytes, this.maximumBytes)) { + this.clear(project); return { stored: false, reason: 'capacity' }; + } + this.write(path.join(this.directory, this.key(project) + '.json'), text); + const evictedProjects = this.trim(); this.error = null; + return { stored: true, reason: 'stored', evictedProjects }; + } catch { this.error = '派生缓存写入失败;当前索引仍来自本次源码核对。'; return { stored: false, reason: 'unavailable' }; } + } + records() { + if (!this.directory) return []; + this.assertDirectory(); + return fs.readdirSync(this.directory).filter(name => recordName.test(name)).map(name => { + const filename = path.join(this.directory, name), stat = this.assertFile(filename); + return { filename, bytes: stat.size, touched: stat.mtimeMs }; + }); + } + trim() { + const records = this.records().sort((a, b) => a.touched - b.touched || a.filename.localeCompare(b.filename)); + let bytes = records.reduce((sum, value) => sum + value.bytes, 0), evicted = 0; + while (records.length > this.maximumProjects || bytes > this.maximumBytes) { + const record = records.shift(); this.assertFile(record.filename); fs.unlinkSync(record.filename); bytes -= record.bytes; evicted++; + } + return evicted; + } + clear(project) { + if (!this.directory) return 0; + this.assertDirectory(); + const names = project ? [path.join(this.directory, this.key(project) + '.json')] : this.records().map(value => value.filename); + let removed = 0; + for (const filename of names) if (fs.existsSync(filename)) { this.assertFile(filename); fs.unlinkSync(filename); removed++; } + return removed; + } + configure(enabled) { + if (typeof enabled !== 'boolean') throw new Error('缓存开关需要布尔值。'); + if (this.directory) this.write(path.join(this.directory, 'settings.json'), JSON.stringify({ schemaVersion: 1, enabled })); + this.enabled = enabled; this.error = null; + } + status() { + let diskProjects = 0, diskBytes = 0; + try { const records = this.records(); diskProjects = records.length; diskBytes = records.reduce((sum, value) => sum + value.bytes, 0); } + catch { this.error = '派生缓存目录或文件无法核对;读取失败时将重建。'; } + return { enabled: this.enabled, persistent: !!this.directory, diskProjects, diskBytes, maximumProjects: this.maximumProjects, + maximumBytes: this.maximumBytes, maximumEntryBytes: this.maximumEntryBytes, error: this.error }; + } +} diff --git a/src/core/context/project-index.mjs b/src/core/context/project-index.mjs index fdead927..445c65d8 100644 --- a/src/core/context/project-index.mjs +++ b/src/core/context/project-index.mjs @@ -3,6 +3,9 @@ import { createHash } from 'node:crypto'; import { parse } from '@babel/parser'; import { inspectProject } from '../project-inspector.mjs'; import { readText } from '../../server/files.mjs'; +import { performance } from 'node:perf_hooks'; +import { IndexCache } from './index-cache.mjs'; +import { projectWorkspaceIdentity } from '../../shared/contracts/workspaces.mjs'; const hash = value => createHash('sha256').update(value).digest('hex'); const conflict = message => Object.assign(new Error(message), { status: 409 }); @@ -139,10 +142,21 @@ function resolveImport(from, dependency, files) { } export class ProjectIndex { - constructor({ maximumProjects = 8, maximumFileBytes = 256000, maximumSymbols = 6000, maximumSymbolsPerFile = 300, maximumAstNodes = 80000 } = {}) { + constructor({ maximumProjects = 8, maximumFileBytes = 256000, maximumSymbols = 6000, maximumSymbolsPerFile = 300, maximumAstNodes = 80000, + cacheDirectory, maximumCacheBytes = 32 * 1024 * 1024, maximumEntryBytes = 8 * 1024 * 1024, parserRevision = 'index-2-python-candidates-1' } = {}) { this.maximumProjects = maximumProjects; this.maximumFileBytes = maximumFileBytes; this.maximumSymbols = maximumSymbols; this.maximumSymbolsPerFile = maximumSymbolsPerFile; this.maximumAstNodes = maximumAstNodes; - this.projects = new Map(); this.pending = new Map(); + this.projects = new Map(); this.pending = new Map(); this.identities = new Map(); this.latest = new Map(); this.memoryBytes = new Map(); this.generation = 0; + this.cache = new IndexCache({ directory: cacheDirectory, maximumProjects, maximumBytes: maximumCacheBytes, maximumEntryBytes }); + this.contract = { parser: parserVersion, revision: parserRevision, maximumFileBytes, maximumSymbols, maximumSymbolsPerFile, maximumAstNodes }; + } + status(project) { return { ...this.cache.status(), memoryProjects: this.projects.size, memoryBytes: [...this.memoryBytes.values()].reduce((sum, bytes) => sum + bytes, 0), pending: this.pending.size, + latest: project ? this.latest.get(project.id + '\0' + project.root) ?? null : null }; } + configure(enabled) { this.cache.configure(enabled); this.generation++; this.projects.clear(); this.identities.clear(); this.memoryBytes.clear(); this.latest.clear(); return this.status(); } + clear(project) { + const removedProjects = this.cache.clear(project), key = project.id + '\0' + project.root; + this.generation++; this.projects.delete(key); this.identities.delete(key); this.memoryBytes.delete(key); this.latest.delete(key); + return { ...this.status(project), clearedAt: new Date().toISOString(), removedProjects }; } refresh(project) { const key = project.id + '\0' + project.root; @@ -153,21 +167,30 @@ export class ProjectIndex { return this.pending.get(key); } async build(project, key) { - const inspection = await inspectProject(project, { includeFiles: true }); - const previous = this.projects.get(key) ?? new Map(), next = new Map(), entries = []; - let updatedFiles = 0, reusedFiles = 0; + const started = performance.now(), generation = this.generation; + const inspection = await inspectProject(project, { includeFiles: true, strictSnapshot: true }); + const identity = { project: projectWorkspaceIdentity(project), contract: this.contract, + git: { available: inspection.git.available, head: inspection.git.head, branch: inspection.git.branch } }; + const memory = this.projects.get(key), sameIdentity = JSON.stringify(this.identities.get(key)) === JSON.stringify(identity); + const loaded = !this.cache.enabled ? { entries: null, reason: 'disabled' } : !inspection.scope.complete ? { entries: null, reason: 'incomplete-scan' } + : memory && sameIdentity ? { entries: memory, reason: 'memory' } : this.cache.load(project, identity); + const previous = loaded.entries ?? new Map(), next = new Map(), entries = []; + let updatedFiles = 0, reusedFiles = 0, parsedFiles = 0, parseMs = 0, invalidatedFiles = 0; for (const file of inspection.files) { if (file.missing) continue; let entry = previous.get(file.path); - if (entry?.sha256 === file.sha256) reusedFiles++; + if (entry?.sha256 === file.sha256 && entry.access === file.access && entry.bytes === file.bytes && this.validEntry(entry)) reusedFiles++; else { + if (entry) invalidatedFiles++; updatedFiles++; entry = { ...file, symbols: [], imports: [], status: 'metadata-only', method: 'file-sha256', truncated: false }; if ((js.test(file.path) || /\.py$/i.test(file.path)) && file.bytes <= this.maximumFileBytes) { try { const text = readText(project, file.path); if (text.sha256 !== file.sha256) throw conflict('建立索引期间文件已变化,请刷新工程索引:' + file.path); - Object.assign(entry, js.test(file.path) ? javascript(text.content, file.path, this.maximumAstNodes, this.maximumSymbolsPerFile) : python(text.content, this.maximumSymbolsPerFile)); + const parseStarted = performance.now(); parsedFiles++; + try { Object.assign(entry, js.test(file.path) ? javascript(text.content, file.path, this.maximumAstNodes, this.maximumSymbolsPerFile) : python(text.content, this.maximumSymbolsPerFile)); } + finally { parseMs += performance.now() - parseStarted; } entry.status = entry.truncated ? 'partial' : 'indexed'; } catch (error) { if (error.status === 409) throw error; @@ -177,14 +200,30 @@ export class ProjectIndex { } next.set(file.path, entry); entries.push(entry); } + // Recheck the bounded snapshot after parsing/loading, including names, Git + // identity and access scope. A cache hit cannot close a concurrent change. + const verified = await inspectProject(project, { includeFiles: true, strictSnapshot: true }); + if (verified.sourceFingerprint !== inspection.sourceFingerprint || JSON.stringify(verified.files) !== JSON.stringify(inspection.files) + || verified.git.available !== inspection.git.available || verified.git.head !== inspection.git.head || verified.git.branch !== inspection.git.branch || verified.git.complete !== inspection.git.complete) { + this.projects.delete(key); this.identities.delete(key); this.memoryBytes.delete(key); + throw conflict('索引核对期间源码、分支或读取范围已变化,请重新刷新;未使用或保存本次派生缓存。'); + } const available = new Map(entries.map(file => [process.platform === 'win32' ? file.path.toLowerCase() : file.path, file])); const allSymbols = entries.flatMap(file => file.symbols.map(value => ({ ...value, path: file.path, sha256: file.sha256, language: file.language }))); const symbols = allSymbols.slice(0, this.maximumSymbols).sort((a, b) => a.path.localeCompare(b.path) || a.line - b.line || a.name.localeCompare(b.name)); const allImports = entries.flatMap(file => file.imports.map(value => resolveImport(file.path, value, available))); const imports = allImports.slice(0, 6000); const removedFiles = [...previous.keys()].filter(file => !next.has(file)).length; - this.projects.delete(key); this.projects.set(key, next); - while (this.projects.size > this.maximumProjects) this.projects.delete(this.projects.keys().next().value); + const entryBytes = Buffer.byteLength(JSON.stringify([...next.values()])), withinCapacity = entryBytes <= Math.min(this.cache.maximumEntryBytes, this.cache.maximumBytes); + const canStore = this.cache.enabled && inspection.scope.complete && generation === this.generation && withinCapacity; + let stored = { stored: false, reason: generation !== this.generation ? 'cleared-during-refresh' : !withinCapacity ? 'capacity' : loaded.reason }; + if (canStore) { + this.projects.delete(key); this.projects.set(key, next); this.identities.set(key, identity); this.memoryBytes.set(key, entryBytes); + while (this.projects.size > this.maximumProjects || [...this.memoryBytes.values()].reduce((sum, bytes) => sum + bytes, 0) > this.cache.maximumBytes) { + const oldest = this.projects.keys().next().value; this.projects.delete(oldest); this.identities.delete(oldest); this.memoryBytes.delete(oldest); this.latest.delete(oldest); + } + stored = this.cache.save(project, identity, next); + } else { this.projects.delete(key); this.identities.delete(key); this.memoryBytes.delete(key); } const warnings = entries.filter(file => ['unindexed', 'partial', 'outside-index-budget'].includes(file.status)).map(file => ({ path: file.path, sha256: file.sha256, status: file.status, message: file.error ?? (file.opaqueImports ? file.opaqueImports + '处动态导入无法确定静态文件目标;其余声明结果保留。' : '该文件达到索引读取或分析上限;按范围提供已有结果。'), line: file.errorLine ?? 1 })); const files = entries.map(({ symbols: ignoredSymbols, imports: ignoredImports, error, ...file }) => file); @@ -192,7 +231,16 @@ export class ProjectIndex { astFiles: entries.filter(file => file.method === 'javascript-typescript-ast').length, pythonCandidateFiles: entries.filter(file => file.method === 'python-declaration-candidate').length, unindexedFiles: warnings.length, truncated: !inspection.scope.complete || warnings.length > 0 || allSymbols.length > symbols.length || allImports.length > imports.length, maximumFileBytes: this.maximumFileBytes, maximumSymbols: this.maximumSymbols }; - return { schemaVersion: 1, projectId: project.id, projectName: project.name, parser: parserVersion, generatedAt: new Date().toISOString(), + const cache = { ...this.cache.status(), checkedAt: verified.inspectedAt, sourceVerified: inspection.scope.complete && verified.scope.complete, + origin: loaded.reason, invalidation: !inspection.scope.complete ? 'incomplete-scan' : memory && !sameIdentity ? 'identity-changed' + : invalidatedFiles ? 'source-or-access-changed' : previous.size && (removedFiles || updatedFiles) ? 'files-added-or-removed' + : ['corrupt', 'identity-changed', 'unavailable'].includes(loaded.reason) ? loaded.reason : null, + reusedFiles, rebuiltFiles: updatedFiles, parsedFiles, invalidatedFiles, removedFiles, verifiedFiles: verified.files.filter(file => file.sha256).length, verificationPasses: 2, + sourceBytesRead: inspection.scope.bytes + verified.scope.bytes, parseMs, durationMs: performance.now() - started, + stored: stored.stored, storageReason: stored.reason, evictedProjects: stored.evictedProjects ?? 0 }; + this.latest.delete(key); if (generation === this.generation) this.latest.set(key, cache); + while (this.latest.size > this.maximumProjects) this.latest.delete(this.latest.keys().next().value); + return { schemaVersion: 1, projectId: project.id, projectName: project.name, parser: parserVersion, generatedAt: new Date().toISOString(), cache, sourceFingerprint: inspection.sourceFingerprint, indexFingerprint: hash(JSON.stringify({ projectId: project.id, parser: parserVersion, source: inspection.sourceFingerprint, symbols, imports, coverage })), sourceScope: inspection.scope, coverage, refresh: { updatedFiles, reusedFiles, removedFiles }, files, symbols, imports, warnings, notes: ['JS/TS/React符号来自静态AST;局部文件候选不证明实际运行时模块解析。', 'Python结果为声明候选,需要读取源文件核对;未执行项目代码。', '只覆盖已读取范围,不解析动态绑定、路径别名或整个语言的引用关系。'] }; @@ -200,7 +248,20 @@ export class ProjectIndex { summary(index) { return { schemaVersion: index.schemaVersion, projectId: index.projectId, projectName: index.projectName, parser: index.parser, generatedAt: index.generatedAt, sourceFingerprint: index.sourceFingerprint, indexFingerprint: index.indexFingerprint, coverage: index.coverage, refresh: index.refresh, - sourceScope: index.sourceScope, warnings: index.warnings.slice(0, 20), notes: index.notes }; + sourceScope: index.sourceScope, warnings: index.warnings.slice(0, 20), notes: index.notes, ...(index.cache ? { cache: index.cache } : {}) }; + } + validEntry(entry) { + const bounded = value => typeof value === 'string' && value.length <= 10000; + return Array.isArray(entry.symbols) && entry.symbols.length <= this.maximumSymbolsPerFile + && entry.symbols.every(value => value && bounded(value.name) && bounded(value.qualifiedName) && bounded(value.kind) && bounded(value.signature) + && Number.isInteger(value.column) && value.column > 0 && Number.isInteger(value.line) && value.line > 0 + && Number.isInteger(value.endLine) && value.endLine >= value.line && typeof value.exported === 'boolean' && bounded(value.evidenceMethod)) + && Array.isArray(entry.imports) && entry.imports.length <= 400 + && entry.imports.every(value => value && bounded(value.source) && bounded(value.kind) && bounded(value.evidenceMethod) && Number.isInteger(value.line) && value.line > 0 + && Array.isArray(value.bindings) && value.bindings.length <= 30 && value.bindings.every(bounded)) + && ['metadata-only', 'indexed', 'partial', 'outside-index-budget'].includes(entry.status) + && ['file-sha256', 'javascript-typescript-ast', 'python-declaration-candidate'].includes(entry.method) + && typeof entry.truncated === 'boolean'; } query(index, { query = '', kind = 'all', limit = 30 } = {}) { if (typeof query !== 'string' || query.length > 200 || !['all', 'symbols', 'imports', 'files'].includes(kind) diff --git a/src/core/project-inspector.mjs b/src/core/project-inspector.mjs index 022e6890..108a0f33 100644 --- a/src/core/project-inspector.mjs +++ b/src/core/project-inspector.mjs @@ -58,7 +58,7 @@ function framework(script) { return null; } -export async function inspectProject(project, { includeFiles = false } = {}) { +export async function inspectProject(project, { includeFiles = false, strictSnapshot = false } = {}) { const root = safePath(project), git = await gitBaseline(project); const inventory = git.paths ? null : listFiles(project, '', 8, 1500); const candidates = (git.paths ?? inventory.files.filter(file => file.kind === 'file').map(file => file.path)).slice().sort(); @@ -67,6 +67,13 @@ export async function inspectProject(project, { includeFiles = false } = {}) { for (const relative of candidates.slice(0, 1000)) { if (excludedFromInventory(relative)) { excluded.push(relative); continue; } try { + if (strictSnapshot) { + let component = root; + for (const part of relative.replaceAll('\\', '/').split('/')) { + component = path.join(component, part); + if (fs.existsSync(component) && fs.lstatSync(component).isSymbolicLink()) throw new Error('索引来源经过链接,不能使用旧解析材料。'); + } + } const file = safePath(project, relative, true); if (!fs.existsSync(file)) { fingerprints.push([relative, 'missing']); sourceFiles.push({ path: relative, missing: true, sha256: null, bytes: 0 }); continue; } const info = fs.lstatSync(file); @@ -75,10 +82,11 @@ export async function inspectProject(project, { includeFiles = false } = {}) { const language = languages[path.extname(relative).toLowerCase()]; if (language) detected.add(language); if (info.size > 1024 * 1024 || bytes + info.size > 16 * 1024 * 1024) { complete = false; fingerprints.push([relative, info.size, 'outside-read-budget']); continue; } const contents = fs.readFileSync(file), sha256 = hash(contents); bytes += contents.length; fingerprints.push([relative, sha256]); - sourceFiles.push({ path: relative, sha256, bytes: contents.length, language: language ?? null }); + sourceFiles.push({ path: relative, sha256, bytes: contents.length, language: language ?? null, + ...(strictSnapshot ? { access: hash(JSON.stringify([info.dev, info.ino, info.mode, info.uid, info.gid, info.nlink])) } : {}) }); } catch (error) { excluded.push(relative); - if (error.message !== '该路径不在可读取的项目范围内。') { complete = false; fingerprints.push([relative, 'unreadable']); } + if (strictSnapshot || error.message !== '该路径不在可读取的项目范围内。') { complete = false; fingerprints.push([relative, 'unreadable']); } } } const packageFile = declaration(project, 'package.json'); diff --git a/src/server/runtime.mjs b/src/server/runtime.mjs index 41bdb43b..2aa66b6f 100644 --- a/src/server/runtime.mjs +++ b/src/server/runtime.mjs @@ -111,7 +111,7 @@ export class Runtime { isActive(id) { return this.active.has(id) || !!this.workers?.isActive(id) || !!this.controlledEngines?.isActive(id) || !!this.checks?.isActive(id) || !!this.deliveries?.isActive(id) || !!this.changeSets?.isActive(id) || !!this.workspaces?.isActive(id) || !!this.research?.isActive(id) || !!this.engines?.isActive(id) || !!this.browser?.isActive(id) || !!this.taskPlans?.isActive(id); } hasActivity() { return (this.provider.activeRequests ?? 0) > 0 || (this.provider.hub?.waiting ?? 0) > 0 || this.active.size > 0 || (this.workers?.active.size ?? 0) > 0 || (this.controlledEngines?.drivers.size ?? 0) > 0 || (this.mcp?.active.size ?? 0) > 0 || (this.checks?.active.size ?? 0) > 0 || (this.deliveries?.active.size ?? 0) > 0 || (this.changeSets?.projects.size ?? 0) > 0 || (this.workspaces?.projects.size ?? 0) > 0 || (this.workspaces?.pending.size ?? 0) > 0 || (this.research?.active.size ?? 0) > 0 || (this.engines?.active.size ?? 0) > 0 || (this.browser?.active.size ?? 0) > 0 || (this.taskPlans?.active.size ?? 0) > 0; } workerManager() { return this.workers ??= new WorkerManager(this.store, this); } - projectIndexer() { return this.codeIndex ??= new ProjectIndex(); } + projectIndexer() { return this.codeIndex ??= new ProjectIndex({ cacheDirectory: path.join(this.store.directory, 'cache', 'project-index') }); } taskPlanner() { return this.taskPlans ??= new TaskPlanManager(this.store, { redact: value => this.provider.redact(value), projectBusy: (projectId, exceptId) => this.projectHasActivity(projectId, exceptId) }); } browserManager() { return this.browser ??= new BrowserManager(this.store, { appDirectory: this.appDirectory, diff --git a/task_plan.md b/task_plan.md index 4fc58cdc..87cee99d 100644 --- a/task_plan.md +++ b/task_plan.md @@ -5,9 +5,9 @@ 用户授权下一阶段全量开发并及时回顾。基线606b5ec,分支feat/project-cache-1.19;按docs/PLAN-1.19.md完成A–F。正式服务及Provider、项目/会话/工件均保留,不提取Key或发起真实模型调用。QA使用独立数据、端口和固定HTTP响应;不新建代理、聊天、goal或automation,不远程push。 1. [complete] A:现有复用/记忆/请求工件已审查;18次固定Node/React/Python测量及原输入SHA保存,见QA-1.19。跨实例AST重解析有收益,Python收益较小。 -2. [in_progress] B:依据测量扩展有收益的有界派生缓存,绑定项目根、SHA、解析器/格式和预算;损坏回退重建。 -3. [pending] C:核对同大小mtime、增删改名、Git分支、权限/链接、不完整扫描和并发变化;完成工具与原冻结请求保持。 -4. [pending] D:既有工程索引/运行诊断显示核对、命中/重建/失效原因、容量、关闭/清除;桌面和390px实际操作验证,手机不扩权。 +2. [complete] B:单一ProjectIndex增加派生持久缓存,项目/解析契约绑定,8项目/32MiB/单项8MiB;损坏和超限回退当前重建。 +3. [complete] C:11项新增+11项旧索引22/22通过;同大小mtime、增删改名、分支、mode/EACCES、junction、不完整及并发变化拒绝/降级。 +4. [in_progress] D:既有工程索引/运行诊断显示核对、命中/重建/失效原因、容量、关闭/清除;桌面和390px实际操作验证,手机不扩权。 5. [pending] E:同题冷/热/失效比较、Node8/8、React9/9、Python10/10、构建/编译/页面/Git及原修改保护;本地缓存与token用量分开。 6. [pending] F:完整历史回归、独立源码/便携/同载荷安装、保留全状态升级、原生回看、证据封存、下一计划及本地Git收尾。 diff --git a/tests/project-cache.test.mjs b/tests/project-cache.test.mjs new file mode 100644 index 00000000..4a7bc30c --- /dev/null +++ b/tests/project-cache.test.mjs @@ -0,0 +1,163 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import fs from 'node:fs'; +import path from 'node:path'; +import { execFileSync } from 'node:child_process'; +import { createHash } from 'node:crypto'; +import { ProjectIndex } from '../src/core/context/project-index.mjs'; +import { fixture } from './helpers.mjs'; + +const setup = t => { + const f = fixture(t), cacheDirectory = path.join(f.root, 'derived'); + const write = (name, text) => { const filename = path.join(f.projectRoot, name); fs.mkdirSync(path.dirname(filename), { recursive: true }); fs.writeFileSync(filename, text); return filename; }; + write('main.ts', 'import { value } from "./value.js";\nexport function read() { return value; }\n'); + write('value.ts', 'export const value = 1;\n'); + const create = (options = {}) => new ProjectIndex({ cacheDirectory, ...options }); + const records = () => fs.readdirSync(cacheDirectory).filter(name => /^[a-f0-9]{64}\.json$/.test(name)); + return { ...f, write, create, records, cacheDirectory }; +}; + +test('persistent parsing is project/SHA/config bound and produces identical semantic indexes after restart', async t => { + const f = setup(t), firstIndexer = f.create(), first = await firstIndexer.refresh(f.project); + assert.equal(first.cache.parsedFiles, 2); assert.equal(first.cache.stored, true); assert.equal(f.records().length, 1); + const second = await f.create().refresh(f.project); + assert.equal(second.cache.origin, 'disk'); assert.equal(second.cache.parsedFiles, 0); assert.equal(second.cache.reusedFiles, 3); + assert.equal(second.cache.sourceVerified, true); assert.equal(second.cache.verificationPasses, 2); + assert.equal(second.indexFingerprint, first.indexFingerprint); assert.deepEqual(second.symbols, first.symbols); assert.deepEqual(second.imports, first.imports); + assert.equal((await firstIndexer.refresh(f.project)).cache.origin, 'memory'); + const otherId = await f.create().refresh({ ...f.project, id: 'different-project' }); + assert.equal(otherId.cache.parsedFiles, 2); assert.notEqual(otherId.indexFingerprint, first.indexFingerprint); + const options = await f.create({ maximumSymbolsPerFile: 1 }).refresh(f.project); + assert.equal(options.cache.origin, 'identity-changed'); assert.equal(options.cache.parsedFiles, 2); + const revision = await f.create({ parserRevision: 'next-parser' }).refresh(f.project); + assert.equal(revision.cache.origin, 'identity-changed'); assert.equal(revision.cache.parsedFiles, 2); +}); + +test('same-size restored-mtime changes, additions, renames and deletions cannot reuse stale entries or import targets', async t => { + const f = setup(t), indexer = f.create(), original = await indexer.refresh(f.project); + const filename = path.join(f.projectRoot, 'value.ts'), stat = fs.statSync(filename); + f.write('value.ts', 'export const other = 2;\n'); fs.utimesSync(filename, stat.atime, stat.mtime); + assert.equal(fs.statSync(filename).size, stat.size); assert.ok(Math.abs(fs.statSync(filename).mtimeMs - stat.mtimeMs) < 1); + const changed = await indexer.refresh(f.project); + assert.equal(changed.cache.invalidatedFiles, 1); assert.equal(changed.cache.parsedFiles, 1); + assert.equal(changed.cache.invalidation, 'source-or-access-changed'); assert.notEqual(changed.sourceFingerprint, original.sourceFingerprint); + assert.ok(changed.symbols.some(value => value.name === 'other')); assert.ok(!changed.symbols.some(value => value.name === 'value')); + fs.renameSync(filename, path.join(f.projectRoot, 'moved.ts')); + const renamed = await f.create().refresh(f.project); + assert.equal(renamed.cache.removedFiles, 1); assert.equal(renamed.imports[0].target, null); assert.equal(renamed.cache.parsedFiles, 1); + f.write('value.ts', 'export const value = 9;\n'); + const added = await indexer.refresh(f.project); assert.equal(added.imports[0].target, 'value.ts'); + fs.unlinkSync(path.join(f.projectRoot, 'moved.ts')); + assert.equal((await indexer.refresh(f.project)).cache.removedFiles, 1); + assert.equal(original.imports[0].target, 'value.ts'); assert.ok(original.symbols.some(value => value.name === 'value')); +}); + +test('branch and commit changes invalidate even byte-identical sources', async t => { + const f = setup(t), git = args => execFileSync('git', ['-C', f.projectRoot, ...args], { windowsHide: true, encoding: 'utf8' }); + git(['init', '--initial-branch=main']); git(['add', '.']); + git(['-c', 'user.name=QA', '-c', 'user.email=qa@local.invalid', 'commit', '-m', 'cache baseline']); + const indexer = f.create(), first = await indexer.refresh(f.project); + git(['switch', '-c', 'other']); + const branch = await indexer.refresh(f.project); + assert.equal(branch.cache.invalidation, 'identity-changed'); assert.equal(branch.cache.parsedFiles, 2); + assert.equal(branch.indexFingerprint, first.indexFingerprint, 'semantic index can match while branch-specific reuse is invalidated'); + git(['-c', 'user.name=QA', '-c', 'user.email=qa@local.invalid', 'commit', '--allow-empty', '-m', 'new identity']); + const commit = await f.create().refresh(f.project); assert.equal(commit.cache.origin, 'identity-changed'); assert.equal(commit.cache.parsedFiles, 2); +}); + +test('corrupt, malformed and schema-invalid records fall back to fresh parsing', async t => { + const f = setup(t), first = await f.create().refresh(f.project), filename = path.join(f.cacheDirectory, f.records()[0]); + fs.writeFileSync(filename, '{bad json'); + const corrupt = await f.create().refresh(f.project); assert.equal(corrupt.cache.parsedFiles, 2); assert.equal(corrupt.indexFingerprint, first.indexFingerprint); + const envelope = JSON.parse(fs.readFileSync(filename, 'utf8')); envelope.payload.entries.find(value => value.path === 'main.ts').symbols = [null]; + envelope.sha256 = createHash('sha256').update(JSON.stringify(envelope.payload)).digest('hex'); fs.writeFileSync(filename, JSON.stringify(envelope)); + const malformed = await f.create().refresh(f.project); assert.equal(malformed.cache.parsedFiles, 1); assert.equal(malformed.indexFingerprint, first.indexFingerprint); + const changed = JSON.parse(fs.readFileSync(filename, 'utf8')); changed.payload.entries[0].sha256 = '0'.repeat(64); fs.writeFileSync(filename, JSON.stringify(changed)); + const digest = await f.create().refresh(f.project); assert.equal(digest.cache.origin, 'corrupt'); assert.equal(digest.cache.parsedFiles, 2); +}); + +test('disable and clear persist without touching sources, sessions, configuration or original artifacts', async t => { + const f = setup(t), indexer = f.create(); await indexer.refresh(f.project); + const before = fs.readFileSync(path.join(f.projectRoot, 'main.ts')), count = f.store.sessions.size, configuration = JSON.stringify(f.store.config); + indexer.configure(false); assert.equal(f.create().status().enabled, false); + const disabled = await indexer.refresh(f.project); assert.equal(disabled.cache.origin, 'disabled'); assert.equal(disabled.cache.parsedFiles, 2); + assert.equal(indexer.projects.size, 0); indexer.configure(true); + assert.equal((await indexer.refresh(f.project)).cache.origin, 'disk'); + assert.equal(indexer.clear(f.project).removedProjects, 1); assert.equal(f.records().length, 0); + assert.equal((await f.create().refresh(f.project)).cache.origin, 'cold'); + assert.deepEqual(fs.readFileSync(path.join(f.projectRoot, 'main.ts')), before); + assert.equal(f.store.sessions.size, count); assert.equal(JSON.stringify(f.store.config), configuration); +}); + +test('cache capacity bounds disk and memory by bytes and LRU project count', async t => { + const f = setup(t), indexer = f.create({ maximumProjects: 1 }); await indexer.refresh(f.project); + await indexer.refresh({ ...f.project, id: 'other-id' }); assert.equal(f.records().length, 1); assert.equal(indexer.projects.size, 1); + assert.equal((await indexer.refresh(f.project)).cache.origin, 'cold'); + const bounded = f.create({ maximumCacheBytes: 64, maximumEntryBytes: 64 }); + const index = await bounded.refresh(f.project); assert.equal(index.cache.storageReason, 'capacity'); assert.equal(bounded.projects.size, 0); + assert.ok(index.symbols.length > 0, 'capacity only affects derived storage'); +}); + +test('incomplete scans do not load or save cached entries; unavailable files leave the current result scope', async t => { + const f = setup(t), indexer = f.create(); await indexer.refresh(f.project); + f.write('too-large.txt', 'x'.repeat(1024 * 1024 + 1)); + const partial = await indexer.refresh(f.project); assert.equal(partial.cache.origin, 'incomplete-scan'); assert.equal(partial.cache.reusedFiles, 0); + assert.equal(partial.cache.stored, false); assert.equal(partial.cache.sourceVerified, false); assert.equal(indexer.projects.size, 0); + fs.unlinkSync(path.join(f.projectRoot, 'too-large.txt')); + const original = fs.readFileSync; + t.mock.method(fs, 'readFileSync', function (filename, ...args) { + if (filename === path.join(f.projectRoot, 'value.ts')) throw Object.assign(new Error('denied'), { code: 'EACCES' }); + return original.call(this, filename, ...args); + }); + const denied = await indexer.refresh(f.project); assert.equal(denied.coverage.snapshotComplete, false); + assert.ok(!denied.symbols.some(value => value.name === 'value')); assert.equal(denied.cache.reusedFiles, 0); +}); + +test('project and cache junctions cannot expose or delete an outside directory', async t => { + const f = setup(t); await f.create().refresh(f.project); + const outside = path.join(f.root, 'outside'); fs.mkdirSync(outside); fs.writeFileSync(path.join(outside, 'protected.ts'), 'export const outsideOnly = 1;\n'); + fs.symlinkSync(outside, path.join(f.projectRoot, 'linked'), process.platform === 'win32' ? 'junction' : 'dir'); + const indexed = await f.create().refresh(f.project); assert.ok(!JSON.stringify(indexed).includes('outsideOnly')); + const linkedCache = path.join(f.root, 'linked-cache'); fs.symlinkSync(outside, linkedCache, process.platform === 'win32' ? 'junction' : 'dir'); + const unsafe = f.create({ cacheDirectory: linkedCache }); assert.equal(unsafe.status().enabled, false); + assert.throws(() => unsafe.clear(f.project)); assert.equal(fs.readFileSync(path.join(outside, 'protected.ts'), 'utf8'), 'export const outsideOnly = 1;\n'); +}); + +test('concurrent file changes refuse a snapshot and clear during pending work cannot repopulate it', async t => { + const f = setup(t), filename = path.join(f.projectRoot, 'value.ts'), original = fs.readFileSync; + let changed = false; + t.mock.method(fs, 'readFileSync', function (name, ...args) { + const result = original.call(this, name, ...args); + if (name === filename && !changed) { changed = true; f.write('value.ts', 'export const fresh = 2;\n'); } + return result; + }); + const indexer = f.create(); await assert.rejects(indexer.refresh(f.project), /已变化/); + assert.equal(indexer.projects.size, 0); assert.equal(f.records().length, 0); + t.mock.restoreAll(); + const pending = indexer.refresh(f.project); assert.equal(indexer.refresh(f.project), pending); indexer.clear(f.project); + const result = await pending; assert.equal(result.cache.storageReason, 'cleared-during-refresh'); assert.equal(f.records().length, 0); assert.equal(indexer.projects.size, 0); + assert.ok(result.symbols.some(value => value.name === 'fresh')); +}); + +test('file access mode changes invalidate parsed material even when bytes are unchanged', async t => { + const f = setup(t), filename = path.join(f.projectRoot, 'value.ts'), indexer = f.create(); + await indexer.refresh(f.project); const old = fs.statSync(filename).mode; + try { + fs.chmodSync(filename, 0o444); + const changed = await indexer.refresh(f.project); + assert.equal(changed.cache.invalidatedFiles, 1); assert.equal(changed.cache.parsedFiles, 1); + } finally { fs.chmodSync(filename, old); } +}); + +test('tracked directory replaced by a junction invalidates access scope and never uses its old cache', async t => { + const f = setup(t); f.write('src/owned.ts', 'export const keepPrivate = 1;\n'); + const git = args => execFileSync('git', ['-C', f.projectRoot, '-c', 'core.autocrlf=false', ...args], { windowsHide: true, encoding: 'utf8' }); + git(['init', '--initial-branch=main']); git(['add', '.']); + git(['-c', 'user.name=QA', '-c', 'user.email=qa@local.invalid', 'commit', '-m', 'tracked tree']); + const indexer = f.create(); await indexer.refresh(f.project); + fs.renameSync(path.join(f.projectRoot, 'src'), path.join(f.root, 'saved-src')); + fs.symlinkSync(path.join(f.root, 'saved-src'), path.join(f.projectRoot, 'src'), process.platform === 'win32' ? 'junction' : 'dir'); + const result = await indexer.refresh(f.project); + assert.equal(result.cache.origin, 'incomplete-scan'); assert.equal(result.cache.sourceVerified, false); assert.equal(result.cache.reusedFiles, 0); + assert.ok(!result.symbols.some(value => value.name === 'keepPrivate')); +});