Skip to content

Commit d65c666

Browse files
committed
fix: derive runtime deploy allowlists from require closure
Add a single runtime-dependency-closure owner that walks hooks/_runtime and mcp require graphs, then derive project and global allowlists from it. Deploy the same closure to .codex/scripts/lib for project Codex hooks, upgrade allowlist-only/layout/packlist tests, and wire the mcp-runtime-closure validation node so new runtime deps cannot silently drift.
1 parent f2192c2 commit d65c666

12 files changed

Lines changed: 496 additions & 154 deletions

‎changelogs/unreleased.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
> **当前**: v1.15.3 已归档到 `changelogs/releases/v1.15.3.md`;本文件仅保留历史锚点索引与镜像供 validate 探针消费。发版后新实现再写入下方「当前未发布」。
55
66
## 当前未发布实现候选
7+
- **Runtime 白名单闭包防复发**:新增 `scripts/lib/runtime-dependency-closure.js` 作为统一 runtime require 闭包 Owner;`MCP_RUNTIME_DEPS` / `PROJECT_RUNTIME_SCRIPT_DEPS`(兼容 `CLAUDE_MCP_RUNTIME_SCRIPT_DEPS`)改为派生;Codex project hook 复制到 `.codex/scripts/lib`;`test:mcp-runtime-closure` 覆盖 allowlist-only layout/MCP smoke/负向/packlist;validation 节点 `mcp-runtime-closure` 接入 fast/full/profile-deploy/package-release。需求:`bugs/runtime白名单闭包防复发修复`。
78
- **P0-1 工作区意图 + DEVCODEX.md + ImplementStartGate + e2e(Batch1)**:入口 `.devcodex/workspace/DEVCODEX.md`;`workspace-skill-intent` catalog/意图/lifecycle;控制面 mutation 前 04+05+复审清单门闩(`IMPLEMENT_START_WITHOUT_PROCESS`);全局 skill `workspace-skill-author`;`npm run test:workspace-skill-intent` / `test:workspace-skill-intent-e2e`(E2E-A~J+K)并入 `test:skill-resolve`。**无 e2e 不算 Batch1 完成**。需求:`开源发布前用户面与文档边界收口`。
89
- **P0-1 Batch2**:`init`/`ensureRuntimeDirs` 写入 DEVCODEX.md 模板(不覆盖);CLI `devcodex skill intent <prompt> [--json]`;help 文案同步。
910
- **P0-1 ECR 计数同步**:新增 `workspace-skill-author` 后 Skill 规模 84→**85**(82 active + 3 gray);重生 `skills/portfolio.json`;同步 test-skill-portfolio / docs-surface-inventory / V92/V95 / README / website 规模锚点。

‎index.js‎

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ const {
3939
resolveActiveRuntimeRoot: sharedResolveActiveRuntimeRoot,
4040
resolveProfileDir: sharedResolveProfileDir
4141
} = require('./hooks/_runtime/workspace-layout.cjs')
42+
const {
43+
collectRuntimeScriptDeps
44+
} = require('./scripts/lib/runtime-dependency-closure.js')
4245
const { createAnsiHelpers, walkDir: walkDirFs } = require('./scripts/lib/cli-console-utils.js')
4346
const c = createAnsiHelpers()
4447
const walkDir = dir => walkDirFs(fs, dir)
@@ -69,21 +72,11 @@ const CLAUDE_SOURCES = [
6972
]
7073

7174
/**
72-
* Allowlist of scripts/lib modules deployed next to Claude MCP servers.
73-
* Keep tight: only modules actually required by mcp/*.js (today: memory + profile).
74-
* Do not ship the entire package scripts/lib tree into consumer projects.
75+
* Tight allowlist of scripts/lib modules deployed next to project hook/MCP runtimes.
76+
* The list is derived from real runtime roots instead of hand-maintained file names.
7577
*/
76-
const CLAUDE_MCP_RUNTIME_SCRIPT_DEPS = Object.freeze([
77-
'scripts/lib/cp-digest.js',
78-
'scripts/lib/host-parity-scorecard.js',
79-
// host-parity-scorecard hard-depends on executable-absorption-gates (profile_compose_entry_check)
80-
'scripts/lib/executable-absorption-gates.js',
81-
'scripts/lib/global-adapter-refresh-guidance.js',
82-
'scripts/lib/global-host-target.js',
83-
'scripts/lib/derived-index-contract.js',
84-
'scripts/lib/memory-index.js',
85-
'scripts/lib/summary-type-canon.js'
86-
])
78+
const PROJECT_RUNTIME_SCRIPT_DEPS = Object.freeze(collectRuntimeScriptDeps(PKG_ROOT))
79+
const CLAUDE_MCP_RUNTIME_SCRIPT_DEPS = PROJECT_RUNTIME_SCRIPT_DEPS
8780

8881
const CODEX_SOURCES = [
8982
{ from: 'hooks/_runtime', to: path.join('.codex', 'hooks', '_runtime') },
@@ -424,7 +417,7 @@ module.exports = {
424417
cmdUninstallHost, cmdGrok, cmdStatus, cmdHelp, cmdProfileInit, cmdDoctor, cmdProbe, cmdTrace,
425418
cmdSkill, cmdTask, cmdGlobalAdapters, isSourceRepo, findLayoutInfo, inferProjectFromCwd, resolveActiveRuntimeRoot,
426419
resolveHostAdapterScope, resolveGitignoreRoot, ensureWorkspaceNamespaceLayout, ensureRuntimeDirs, SOURCES, CLAUDE_SOURCES,
427-
CLAUDE_MCP_RUNTIME_SCRIPT_DEPS, CODEX_SOURCES, CLAUDE_HOOK_COMMAND, CLAUDE_MCP_JSON,
420+
PROJECT_RUNTIME_SCRIPT_DEPS, CLAUDE_MCP_RUNTIME_SCRIPT_DEPS, CODEX_SOURCES, CLAUDE_HOOK_COMMAND, CLAUDE_MCP_JSON,
428421
CODEX_HOOK_COMMAND, buildDeploymentDescriptors, beginManagedDeployment, finishManagedDeployment,
429422
runMigrateLayout
430423
}

‎package.json‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@
188188
"scripts/test-global-host-config.js",
189189
"scripts/test-global-host-runtime-verifier.js",
190190
"scripts/test-global-install-smoke.js",
191+
"scripts/test-mcp-runtime-closure.js",
191192
"scripts/fixtures/global-host-config/",
192193
"scripts/test-security-audit.js",
193194
"scripts/fixtures/security-audit/",
@@ -271,6 +272,7 @@
271272
"scripts/lib/global-host-config-merge.js",
272273
"scripts/lib/global-host-config-transaction.js",
273274
"scripts/lib/global-host-runtime-verifier.js",
275+
"scripts/lib/runtime-dependency-closure.js",
274276
"scripts/lib/grok-workspace-launcher.js",
275277
"scripts/lib/cli-host-utils.js",
276278
"scripts/lib/cli-console-utils.js",

‎scripts/lib/cli-install-commands.js‎

Lines changed: 44 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,39 @@ function buildCliInstallCommands(ctx) {
8585
return envelope
8686
}
8787

88+
function copyRuntimeScriptDeps({ targetRoot, shownRoot, force, dryRun, log, inlineLog = log }) {
89+
const counts = { added: 0, updated: 0, skipped: 0 }
90+
for (const relRaw of CLAUDE_MCP_RUNTIME_SCRIPT_DEPS) {
91+
const rel = String(relRaw || '').replace(/\\/g, '/')
92+
if (!rel) continue
93+
const srcFile = path.join(PKG_ROOT, ...rel.split('/'))
94+
if (!fs.existsSync(srcFile)) {
95+
inlineLog(c.yellow(` ⚠ missing project runtime dep source: ${rel}`))
96+
continue
97+
}
98+
const destFile = path.join(targetRoot, ...rel.split('/'))
99+
const existed = fs.existsSync(destFile)
100+
const shown = `${shownRoot}/${rel}`
101+
if (existed && filesContentEqual(srcFile, destFile)) {
102+
counts.skipped++
103+
log(c.dim(` ~ ${shown}`))
104+
continue
105+
}
106+
if (existed && !force) {
107+
counts.skipped++
108+
log(c.dim(` ~ ${shown} (outdated; use --force)`))
109+
continue
110+
}
111+
if (!dryRun) {
112+
fs.mkdirSync(path.dirname(destFile), { recursive: true })
113+
fs.copyFileSync(srcFile, destFile)
114+
}
115+
if (existed) { counts.updated++; log(c.yellow(` ↺ ${shown}`)) }
116+
else { counts.added++; log(c.green(` ✓ ${shown}`)) }
117+
}
118+
return counts
119+
}
120+
88121
function cmdInitWorkspaceRuntime(argv = [], { refresh = false } = {}) {
89122
const values = Array.isArray(argv) ? argv : []
90123
const json = values.includes('--json')
@@ -620,35 +653,10 @@ function buildCliInstallCommands(ctx) {
620653
}
621654
}
622655

623-
// 2b. MCP runtime script deps: copy scripts/lib helpers for Claude MCP (mcp/*.js relative require path)
624-
for (const relRaw of CLAUDE_MCP_RUNTIME_SCRIPT_DEPS) {
625-
const rel = String(relRaw || '').replace(/\\/g, '/')
626-
if (!rel) continue
627-
const srcFile = path.join(PKG_ROOT, ...rel.split('/'))
628-
if (!fs.existsSync(srcFile)) {
629-
inlineLog(c.yellow(` ⚠ missing MCP runtime dep source: ${rel}`))
630-
continue
631-
}
632-
const destFile = path.join(clDir, ...rel.split('/'))
633-
const existed = fs.existsSync(destFile)
634-
const shown = `.claude/${rel}`
635-
636-
if (existed && filesContentEqual(srcFile, destFile)) {
637-
skipped++
638-
log(c.dim(` ~ ${shown}`))
639-
continue
640-
}
641-
if (existed && !force) {
642-
skipped++
643-
log(c.dim(` ~ ${shown} (outdated; use --force)`))
644-
continue
645-
}
646-
if (!dryRun) {
647-
fs.mkdirSync(path.dirname(destFile), { recursive: true })
648-
fs.copyFileSync(srcFile, destFile)
649-
}
650-
if (existed) { updated++; log(c.yellow(` ↺ ${shown}`)) }
651-
else { added++; log(c.green(` ✓ ${shown}`)) }
656+
// 2b. Project runtime script deps for Claude hooks and shared MCP.
657+
{
658+
const counts = copyRuntimeScriptDeps({ targetRoot: clDir, shownRoot: '.claude', force, dryRun, log, inlineLog })
659+
added += counts.added; updated += counts.updated; skipped += counts.skipped
652660
}
653661

654662
// 3. Write / merge .claude/settings.json
@@ -832,6 +840,11 @@ function buildCliInstallCommands(ctx) {
832840
}
833841
}
834842

843+
{
844+
const counts = copyRuntimeScriptDeps({ targetRoot: path.join(cwd, '.codex'), shownRoot: '.codex', force, dryRun, log, inlineLog })
845+
added += counts.added; updated += counts.updated; skipped += counts.skipped
846+
}
847+
835848
// Deploy shared MCP runtime under .claude/mcp (reused by Claude + Codex)
836849
{
837850
const mcpSrc = path.join(PKG_ROOT, 'mcp')
@@ -860,31 +873,8 @@ function buildCliInstallCommands(ctx) {
860873
else { added++; log(c.green(` ✓ ${shown}`)) }
861874
}
862875
}
863-
for (const relRaw of CLAUDE_MCP_RUNTIME_SCRIPT_DEPS) {
864-
const rel = String(relRaw || '').replace(/\\/g, '/')
865-
if (!rel) continue
866-
const srcFile = path.join(PKG_ROOT, ...rel.split('/'))
867-
if (!fs.existsSync(srcFile)) continue
868-
const destFile = path.join(cwd, '.claude', ...rel.split('/'))
869-
const existed = fs.existsSync(destFile)
870-
const shown = `.claude/${rel}`
871-
if (existed && filesContentEqual(srcFile, destFile)) {
872-
skipped++
873-
log(c.dim(` ~ ${shown}`))
874-
continue
875-
}
876-
if (existed && !force) {
877-
skipped++
878-
log(c.dim(` ~ ${shown} (outdated; use --force)`))
879-
continue
880-
}
881-
if (!dryRun) {
882-
fs.mkdirSync(path.dirname(destFile), { recursive: true })
883-
fs.copyFileSync(srcFile, destFile)
884-
}
885-
if (existed) { updated++; log(c.yellow(` ↺ ${shown}`)) }
886-
else { added++; log(c.green(` ✓ ${shown}`)) }
887-
}
876+
const counts = copyRuntimeScriptDeps({ targetRoot: path.join(cwd, '.claude'), shownRoot: '.claude', force, dryRun, log, inlineLog })
877+
added += counts.added; updated += counts.updated; skipped += counts.skipped
888878
}
889879

890880
// Merge Codex config.toml MCP servers (managed block; fail-closed; does not wipe user keys)

‎scripts/lib/deployment-descriptors.js‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function buildDeploymentDescriptors(packageRoot, surfaces, {
4545
}
4646
if (selected.has('claude')) {
4747
descriptors.push(...CLAUDE_SOURCES.map(item => descriptor('claude', item.from, path.join('.claude', item.to))))
48-
// MCP runtime script deps for Claude: map package scripts/lib into .claude/scripts/lib
48+
// Project runtime script deps for Claude hooks + shared MCP: map package scripts/lib into .claude/scripts/lib.
4949
for (const rel of CLAUDE_MCP_RUNTIME_SCRIPT_DEPS) {
5050
const portable = String(rel || '').replace(/\\/g, '/')
5151
if (!portable) continue
@@ -71,6 +71,17 @@ function buildDeploymentDescriptors(packageRoot, surfaces, {
7171
))
7272
}
7373
}
74+
// Codex project hooks resolve ../../scripts/lib from .codex/hooks/_runtime.
75+
for (const rel of CLAUDE_MCP_RUNTIME_SCRIPT_DEPS) {
76+
const portable = String(rel || '').replace(/\\/g, '/')
77+
if (!portable) continue
78+
descriptors.push(descriptor(
79+
'codex',
80+
portable,
81+
path.join('.codex', ...portable.split('/')),
82+
'codex-hook-runtime-dep'
83+
))
84+
}
7485
// Managed-segment observation: package does not own full user .codex/config.toml as a source copy.
7586
// role documents ownership boundary for doctor/manifest consumers (F-006).
7687
descriptors.push({

‎scripts/lib/global-host-config.js‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,14 @@ const {
2828
const {
2929
describeGlobalAdapterRefreshForPackageRoot
3030
} = require('./global-adapter-refresh-guidance.js')
31+
const {
32+
collectRuntimeScriptDeps
33+
} = require('./runtime-dependency-closure.js')
3134

3235
const GLOBAL_HOST_CONFIG_SCHEMA = 'GlobalOnlyHostConfigModeV1'
3336
const GLOBAL_HOST_RECEIPT_SCHEMA = 'GlobalHostConfigReceiptV1'
34-
const MCP_RUNTIME_DEPS = Object.freeze([
35-
'scripts/lib/cp-digest.js',
36-
'scripts/lib/host-parity-scorecard.js',
37-
'scripts/lib/executable-absorption-gates.js',
38-
'scripts/lib/global-adapter-refresh-guidance.js',
39-
'scripts/lib/global-host-target.js',
40-
'scripts/lib/derived-index-contract.js',
41-
'scripts/lib/memory-index.js',
42-
'scripts/lib/summary-type-canon.js'
43-
])
37+
const PACKAGE_ROOT = path.resolve(__dirname, '..', '..')
38+
const MCP_RUNTIME_DEPS = Object.freeze(collectRuntimeScriptDeps(PACKAGE_ROOT))
4439

4540
function portable(filePath) {
4641
return path.resolve(filePath).replace(/\\/g, '/')

‎scripts/lib/global-host-runtime-verifier.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ function deepGrokProbe(target, cwd, options = {}) {
487487
'native',
488488
`${name}:${smoke.error || smoke.textHead || 'tool-smoke-failed'}`,
489489
smoke.error === 'module-missing'
490-
? 'Sync CLAUDE_MCP_RUNTIME_SCRIPT_DEPS (include executable-absorption-gates.js) and re-apply global runtime.'
490+
? 'Sync runtime dependency closure allowlists and re-apply global runtime.'
491491
: 'Re-run MCP tools/call smoke after fixing runtime; ensure tool returns within timeout.'
492492
))
493493
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
'use strict'
2+
3+
const fs = require('fs')
4+
const path = require('path')
5+
6+
const DEFAULT_RUNTIME_ROOTS = Object.freeze([
7+
'hooks/_runtime',
8+
'mcp'
9+
])
10+
11+
function portable(filePath) {
12+
return String(filePath || '').replace(/\\/g, '/')
13+
}
14+
15+
function walkRuntimeFiles(packageRoot, relativeRoot, fsImpl = fs) {
16+
const root = path.join(packageRoot, ...portable(relativeRoot).split('/'))
17+
if (!fsImpl.existsSync(root)) return []
18+
const stat = fsImpl.statSync(root)
19+
if (stat.isFile()) return /\.(?:cjs|js)$/i.test(root) ? [portable(relativeRoot)] : []
20+
const result = []
21+
const stack = [root]
22+
while (stack.length) {
23+
const current = stack.pop()
24+
for (const entry of fsImpl.readdirSync(current, { withFileTypes: true })) {
25+
const full = path.join(current, entry.name)
26+
if (entry.isDirectory()) stack.push(full)
27+
else if (entry.isFile() && /\.(?:cjs|js)$/i.test(entry.name)) {
28+
result.push(portable(path.relative(packageRoot, full)))
29+
}
30+
}
31+
}
32+
return result.sort()
33+
}
34+
35+
function resolveRelativeModule(packageRoot, fromRelative, request, fsImpl = fs) {
36+
if (!request || !request.startsWith('.')) return null
37+
const fromDir = path.posix.dirname(portable(fromRelative))
38+
const base = path.posix.normalize(path.posix.join(fromDir, request))
39+
const candidates = /\.(?:cjs|js)$/i.test(base)
40+
? [base]
41+
: [`${base}.js`, `${base}.cjs`, path.posix.join(base, 'index.js')]
42+
return candidates.find(candidate => fsImpl.existsSync(path.join(packageRoot, ...candidate.split('/')))) || null
43+
}
44+
45+
function readPackageText(packageRoot, relative, fsImpl = fs) {
46+
return fsImpl.readFileSync(path.join(packageRoot, ...portable(relative).split('/')), 'utf8')
47+
}
48+
49+
function collectRequireRequests(source) {
50+
const requests = []
51+
const re = /require\(\s*['"]([^'"]+)['"]\s*\)/g
52+
let match
53+
while ((match = re.exec(source)) !== null) requests.push(match[1])
54+
return requests
55+
}
56+
57+
function collectRuntimeScriptDeps(packageRoot, options = {}) {
58+
const fsImpl = options.fs || fs
59+
const roots = Array.isArray(options.roots) && options.roots.length
60+
? options.roots
61+
: DEFAULT_RUNTIME_ROOTS
62+
const boundary = portable(options.boundary || 'scripts/lib')
63+
const required = new Set()
64+
const queue = []
65+
const entryFiles = roots.flatMap(root => walkRuntimeFiles(packageRoot, root, fsImpl))
66+
67+
function enqueue(relative) {
68+
const normalized = portable(relative)
69+
if (!normalized.startsWith(`${boundary}/`)) return
70+
if (required.has(normalized)) return
71+
required.add(normalized)
72+
queue.push(normalized)
73+
}
74+
75+
for (const entry of entryFiles) {
76+
const source = readPackageText(packageRoot, entry, fsImpl)
77+
for (const request of collectRequireRequests(source)) {
78+
const resolved = resolveRelativeModule(packageRoot, entry, request, fsImpl)
79+
if (resolved) enqueue(resolved)
80+
}
81+
}
82+
83+
for (let index = 0; index < queue.length; index += 1) {
84+
const current = queue[index]
85+
const source = readPackageText(packageRoot, current, fsImpl)
86+
for (const request of collectRequireRequests(source)) {
87+
const resolved = resolveRelativeModule(packageRoot, current, request, fsImpl)
88+
if (resolved) enqueue(resolved)
89+
}
90+
}
91+
92+
return [...required].sort()
93+
}
94+
95+
function runtimeClosureCoverage(packageRoot, allowlist, options = {}) {
96+
const expected = collectRuntimeScriptDeps(packageRoot, options)
97+
const actual = new Set((allowlist || []).map(portable))
98+
const missing = expected.filter(dep => !actual.has(dep))
99+
const extra = [...actual].filter(dep => dep.startsWith('scripts/lib/') && !expected.includes(dep)).sort()
100+
return { expected, actual: [...actual].sort(), missing, extra }
101+
}
102+
103+
function assertRuntimeClosureCovered(packageRoot, allowlist, options = {}) {
104+
const label = options.label || 'runtime dependency allowlist'
105+
const coverage = runtimeClosureCoverage(packageRoot, allowlist, options)
106+
if (coverage.missing.length) {
107+
const error = new Error(`${label} missing runtime closure deps: ${coverage.missing.join(', ')}`)
108+
error.code = 'RUNTIME_CLOSURE_ALLOWLIST_MISSING'
109+
error.coverage = coverage
110+
throw error
111+
}
112+
return coverage
113+
}
114+
115+
module.exports = {
116+
DEFAULT_RUNTIME_ROOTS,
117+
collectRuntimeScriptDeps,
118+
runtimeClosureCoverage,
119+
assertRuntimeClosureCovered
120+
}

0 commit comments

Comments
 (0)