@tokensapi/dsh-plugin-check 0.3.1 → 0.3.2

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.
package/README.md CHANGED
@@ -1,79 +1,91 @@
1
- # dsh-plugin-check
2
-
3
- TokensCowork / DSH 插件合格性体检。给所有插件开发者的统一标准:一条命令,当场知道你的插件**能否安全装入宿主、会不会把用户的 Cowork 搞崩**。同一套规则同时用于:开发者本地自查、插件仓库 CI、插件市场上架门禁。
4
-
5
- ## 用法
6
-
7
- ```bash
8
- # 在插件目录里
9
- npx @tokensapi/dsh-plugin-check
10
-
11
- # 指定目标运行时(启用兼容范围判定;当前产品运行时版本见市场公告)
12
- npx @tokensapi/dsh-plugin-check --runtime 0.1.3-alpha.1
13
-
14
- # 只跑离线清单体检(秒级,无网络)
15
- npx @tokensapi/dsh-plugin-check --skip-smoke
16
-
17
- # 机器可读输出(CI / 市场门禁)
18
- npx @tokensapi/dsh-plugin-check --json
19
-
20
- # 体检一个已发布的 npm 包(市场上架审核用)
21
- npx @tokensapi/dsh-plugin-check --package @scope/name@1.2.3 --runtime 0.1.3-alpha.1
22
- ```
23
-
24
- 退出码:`0` 合格,`1` 存在 error(`--strict` 时 warning 也计入)。冒烟失败时加 `--keep-workspace` 保留临时工作区排查。
25
-
26
- ## 体检分两个阶段
27
-
28
- **阶段① 清单体检**(纯静态、离线、秒级):检查 package.json 与 cordis 补丁的声明是否合规。
29
- **阶段② 隔离启动冒烟**(联网、约 1–3 分钟):在临时工作区按你声明的 peer 范围装出真实运行时,然后逐补丁行做真实网关启动时做的事——解析入口 → `import` → `new Context()` + `ctx.plugin()` 应用。三段都过,你的插件就不会以"装上即崩"的方式杀死用户的 Cowork。全程 `--ignore-scripts`,你的代码只在一次性子进程里运行,超时即杀。
30
-
31
- 清单存在 error 时冒烟不执行——先修再跑。
32
-
33
- ## 规则清单
34
-
35
- 每条规则都对应一种真实发生过的宿主故障。
36
-
37
- | 规则 | 级别 | 内容 | 拦的是什么事故 |
38
- |---|---|---|---|
39
- | M1-manifest | error/warning | package.json 合法,name/version 齐全;预发布版本给出上架提示 | 无法入库 |
40
- | M2-lifecycle | error | 禁止 `preinstall/install/postinstall/prepare` 脚本(构建用 `prepack`) | 安装即执行任意代码;市场受控安装的既有红线 |
41
- | M3-core-peer | error | `@deepseek-ai/cordis*`、`@deepseek-ai/dsh*` 只能是 peerDependencies | 双内核实例 → Symbol 不等 → 服务注册对不上 → **Cowork 启动失败** |
42
- | M4-bundle | error | `dsh.bundle.patch` 必填、文件存在、能解析出插件行 | 宿主无法把插件挂进加载树 |
43
- | M5-row-scope | error | 补丁行只能指向本包(或其子路径) | 插件行劫持挂载其他包 |
44
- | M6-engine | error/warning | `dsh.engine` 声明目标运行时 SemVer 范围;与 `--runtime` 不相交为 error(未声明当前为 warning,将转必填) | 装进不适配的宿主版本 |
45
- | M7-peer-range | warning | 各 `dsh-*` peer 范围应包含目标运行时 | 宿主升级后接口错配,运行时断裂 |
46
- | M8-node-engines | warning | 建议声明 `engines.node`(宿主为 Node 22+) | 语法/API 不可用 |
47
- | M9-license | warning | 建议声明 license | 分发合规 |
48
- | M10-secrets | error/warning | 发布内容不得包含 `.env`、私钥等凭据样式文件 | 凭据泄露 |
49
- | S1-pack | error | `npm pack --ignore-scripts` 必须成功 | 包本身发布不出来 |
50
- | S2-install | error | 按你声明的依赖必须装得出来 | 用户受控安装会同样失败 |
51
- | S3/S4-apply | error | 每个补丁行:入口可解析、`import` 不抛、`ctx.plugin()` 应用不抛(声明 `inject` 等待服务注入属正常,不算失败) | **启动链击穿**——一行 import 失败会拖死整棵插件树 |
52
-
53
- ## `dsh.engine` 字段
54
-
55
- package.json 声明你适配的 DSH 运行时范围:
56
-
57
- ```json
58
- {
59
- "dsh": {
60
- "engine": ">=0.1.3-alpha.1 <0.2.0",
61
- "bundle": { "patch": "./cordis.patch.yml" }
62
- }
63
- }
64
- ```
65
-
66
- 宿主升级越过你的范围时,市场会把你的插件标记为"待适配"而不是硬载崩溃。
67
-
68
- ## CI 集成示例
69
-
70
- ```yaml
71
- # .github/workflows/check.yml
72
- - run: npx @tokensapi/dsh-plugin-check --runtime 0.1.3-alpha.1 --strict
73
- ```
74
-
75
- ## 上架流程中的位置
76
-
77
- 1. 开发者:发布 npm 版本前本地跑体检,全绿再发。
78
- 2. 管理员:在仓库 Actions 的 **Plugin Check** workflow 填「包名@版本」手动触发,体检报告生成在该次运行的 Summary。
79
- 3. 上架:后台 publish 对话框把这次运行的链接填入「检查记录链接」——链接的红/绿即审核证据,`reviewed_version` 随上架落库。存在 error 的版本不满足上架条件。
1
+ # dsh-plugin-check
2
+
3
+ TokensCowork / DSH 插件合格性体检。给所有插件开发者的统一标准:一条命令,当场知道你的插件**能否安全装入宿主、会不会把用户的 Cowork 搞崩**。同一套规则同时用于:开发者本地自查、插件仓库 CI、插件市场上架门禁。
4
+
5
+ ## 用法
6
+
7
+ ```bash
8
+ # 在插件目录里
9
+ npx @tokensapi/dsh-plugin-check
10
+
11
+ # 指定目标运行时(启用兼容范围判定;当前产品运行时版本见市场公告)
12
+ npx @tokensapi/dsh-plugin-check --runtime 0.1.3-alpha.1
13
+
14
+ # 只跑离线清单体检(秒级,无网络)
15
+ npx @tokensapi/dsh-plugin-check --skip-smoke
16
+
17
+ # 机器可读输出(CI / 市场门禁)
18
+ npx @tokensapi/dsh-plugin-check --json
19
+
20
+ # 体检一个已发布的 npm 包(市场上架审核用)
21
+ npx @tokensapi/dsh-plugin-check --package @scope/name@1.2.3 --runtime 0.1.3-alpha.1
22
+ ```
23
+
24
+ 退出码:`0` 合格,`1` 存在 error(`--strict` 时 warning 也计入)。冒烟失败时加 `--keep-workspace` 保留临时工作区排查。
25
+
26
+ ## 体检分两个阶段
27
+
28
+ **阶段① 清单体检**(纯静态、离线、秒级):检查 package.json 与 cordis 补丁的声明是否合规。
29
+ **阶段② 隔离启动冒烟**(联网、约 1–3 分钟):在临时工作区按你声明的 peer 范围装出真实运行时,然后逐补丁行做真实网关启动时做的事——解析入口 → `import` → `new Context()` + `ctx.plugin()` 应用。三段都过,你的插件就不会以"装上即崩"的方式杀死用户的 Cowork。全程 `--ignore-scripts`,你的代码只在一次性子进程里运行,超时即杀。
30
+
31
+ 清单存在 error 时冒烟不执行——先修再跑。
32
+
33
+ ## 规则清单
34
+
35
+ 每条规则都对应一种真实发生过的宿主故障。
36
+
37
+ | 规则 | 级别 | 内容 | 拦的是什么事故 |
38
+ |---|---|---|---|
39
+ | M1-manifest | error/warning | package.json 合法,name/version 齐全;预发布版本给出上架提示 | 无法入库 |
40
+ | M2-lifecycle | error | 禁止 `preinstall/install/postinstall/prepare` 脚本(构建用 `prepack`) | 安装即执行任意代码;市场受控安装的既有红线 |
41
+ | M3-core-peer | error | `@deepseek-ai/cordis*`、`@deepseek-ai/dsh*` 只能是 peerDependencies | 双内核实例 → Symbol 不等 → 服务注册对不上 → **Cowork 启动失败** |
42
+ | M4-bundle | error | `dsh.bundle.patch` 必填、文件存在、能解析出插件行 | 宿主无法把插件挂进加载树 |
43
+ | M5-row-scope | error | 补丁行只能指向本包(或其子路径) | 插件行劫持挂载其他包 |
44
+ | M6-engine | error/warning | `dsh.engine` 声明目标运行时 SemVer 范围;与 `--runtime` 不相交为 error(未声明当前为 warning,将转必填) | 装进不适配的宿主版本 |
45
+ | M7-peer-range | warning | 各 `dsh-*` peer 范围应包含目标运行时 | 宿主升级后接口错配,运行时断裂 |
46
+ | M8-node-engines | warning | 建议声明 `engines.node`(宿主为 Node 22+) | 语法/API 不可用 |
47
+ | M9-license | warning | 建议声明 license | 分发合规 |
48
+ | M10-secrets | error/warning | 发布内容不得包含 `.env`、私钥等凭据样式文件 | 凭据泄露 |
49
+ | S1-pack | error | `npm pack --ignore-scripts` 必须成功 | 包本身发布不出来 |
50
+ | S2-install | error/warning | 按你声明的依赖必须装得出来;宿主内核包(`@deepseek-ai/*`)的内部版本在公开源取不到时降级为 warning 并跳过冒烟 | 用户受控安装会同样失败 |
51
+ | S3/S4-apply | error | 每个补丁行:入口可解析、`import` 不抛、`ctx.plugin()` 应用不抛(声明 `inject` 等待服务注入属正常,不算失败) | **启动链击穿**——一行 import 失败会拖死整棵插件树 |
52
+
53
+ ## 会话内体检(装进 Cowork)
54
+
55
+ 本包同时是一个 Cowork 插件。装进宿主后会注册 `plugin_check` 工具,在会话里直接问"这个插件能装吗":
56
+
57
+ ```
58
+ plugin_check(package="@scope/name", version="1.2.3")
59
+ ```
60
+
61
+ 会话内跑的是**清单规则 + 包内补丁行检查**:工具会把已发布的 tarball 取下来,解出 `cordis.patch.yml` 真读补丁行——只看 registry 清单会漏掉"补丁行 `name` 写成 cordis 插件名而不是 npm 包名"这类装上即让整棵插件树崩溃的缺陷。取不到包时如实标注补丁未检查(warning),不会因网络问题把插件判成不合格。
62
+
63
+ 隔离启动冒烟无法在宿主进程里执行,完整体检仍走 CLI 或 Plugin Check workflow。
64
+
65
+ ## `dsh.engine` 字段
66
+
67
+ 在 package.json 声明你适配的 DSH 运行时范围:
68
+
69
+ ```json
70
+ {
71
+ "dsh": {
72
+ "engine": ">=0.1.3-alpha.1 <0.2.0",
73
+ "bundle": { "patch": "./cordis.patch.yml" }
74
+ }
75
+ }
76
+ ```
77
+
78
+ 宿主升级越过你的范围时,市场会把你的插件标记为"待适配"而不是硬载崩溃。
79
+
80
+ ## CI 集成示例
81
+
82
+ ```yaml
83
+ # .github/workflows/check.yml
84
+ - run: npx @tokensapi/dsh-plugin-check --runtime 0.1.3-alpha.1 --strict
85
+ ```
86
+
87
+ ## 上架流程中的位置
88
+
89
+ 1. 开发者:发布 npm 版本前本地跑体检,全绿再发。
90
+ 2. 管理员:在仓库 Actions 的 **Plugin Check** workflow 填「包名@版本」手动触发,体检报告生成在该次运行的 Summary。
91
+ 3. 上架:后台 publish 对话框把这次运行的链接填入「检查记录链接」——链接的红/绿即审核证据,`reviewed_version` 随上架落库。存在 error 的版本不满足上架条件。
@@ -1,78 +1,83 @@
1
- /* ============================================================
2
- * Cowork 插件入口:会话内插件体检工具
3
- * ============================================================
4
- * 装进 TokensCowork 后注册 plugin_check 工具:对任意 npm 插件包
5
- * 跑清单规则(与 CLI 的清单阶段同源),模型或用户在会话里即可
6
- * 判断"这个插件装进来会不会有问题"。含隔离启动冒烟的完整体检
7
- * 仍走 CLI:npx @tokensapi/dsh-plugin-check --package <name@ver>。
8
- * ============================================================ */
9
- import { defineTool } from '@deepseek-ai/dsh-tools'
10
- import { checkPublishedManifest } from './registry-check.mjs'
11
-
12
- /** 缺省比对的运行时版本;宿主升级后可通过插件配置覆盖。 */
13
- const DEFAULT_RUNTIME = '0.1.3-alpha.1'
14
- const REGISTRY = 'https://registry.npmjs.org'
15
- const FETCH_TIMEOUT_MS = 15_000
16
-
17
- export const name = 'tokens-plugin-check'
18
- export const inject = ['tools']
19
-
20
- async function fetchManifest(packageName, version) {
21
- const controller = new AbortController()
22
- const timer = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS)
23
- try {
24
- const response = await fetch(
25
- `${REGISTRY}/${encodeURIComponent(packageName)}/${encodeURIComponent(version)}`,
26
- { signal: controller.signal, headers: { accept: 'application/json' } },
27
- )
28
- if (!response.ok) {
29
- throw new Error(response.status === 404 ? `npm 上未找到 ${packageName}@${version}` : `npm 查询失败(HTTP ${response.status})`)
30
- }
31
- return await response.json()
32
- } finally {
33
- clearTimeout(timer)
34
- }
35
- }
36
-
37
- /**
38
- * @param {import('@deepseek-ai/cordis').Context} ctx
39
- * @param {{ runtime?: string }} [config]
40
- */
41
- export function apply(ctx, config = {}) {
42
- const runtime = typeof config.runtime === 'string' && config.runtime !== '' ? config.runtime : DEFAULT_RUNTIME
43
- ctx.tools.register(defineTool({
44
- name: 'plugin_check',
45
- description: 'Check whether a published DSH/TokensCowork plugin npm package is safe to install: '
46
- + 'forbidden lifecycle scripts, dual-kernel core dependencies, missing dsh bundle patch, '
47
- + 'and runtime/peer compatibility against the current DSH runtime. '
48
- + 'Returns ok=false with per-rule findings when the plugin would break the host.',
49
- parameters: {
50
- package: {
51
- type: 'string',
52
- required: true,
53
- description: 'npm package name, e.g. "@tokensapi/dsh-progressive-tools".',
54
- },
55
- version: {
56
- type: 'string',
57
- description: 'Exact version to check. Defaults to the latest dist-tag.',
58
- },
59
- },
60
- output: {
61
- schema: { type: 'json' },
62
- render: (_args, value) => [{ type: 'text', text: JSON.stringify(value) }],
63
- },
64
- isConcurrencySafe: () => true,
65
- async execute(args) {
66
- const manifest = await fetchManifest(args.package, args.version ?? 'latest')
67
- const findings = checkPublishedManifest(manifest, runtime)
68
- return {
69
- package: manifest.name,
70
- version: manifest.version,
71
- runtime,
72
- ok: !findings.some(finding => finding.level === 'error'),
73
- findings,
74
- note: '以上为清单规则;含隔离启动冒烟的完整体检请运行 npx @tokensapi/dsh-plugin-check --package <name@version>',
75
- }
76
- },
77
- }))
78
- }
1
+ /* ============================================================
2
+ * Cowork 插件入口:会话内插件体检工具
3
+ * ============================================================
4
+ * 装进 TokensCowork 后注册 plugin_check 工具:对任意 npm 插件包
5
+ * 跑清单规则 + 包内补丁行检查(与 CLI 的清单阶段同源),模型或
6
+ * 用户在会话里即可判断"这个插件装进来会不会有问题"。补丁行会
7
+ * tarball 里真读出来 —— 只看 registry 清单会漏掉"补丁行 name
8
+ * 写成插件名"这类装上即崩的缺陷。含隔离启动冒烟的完整体检仍走
9
+ * CLI:npx @tokensapi/dsh-plugin-check --package <name@ver>。
10
+ * ============================================================ */
11
+ import { defineTool } from '@deepseek-ai/dsh-tools'
12
+ import { checkPublishedPackage } from './registry-check.mjs'
13
+
14
+ /** 缺省比对的运行时版本;宿主升级后可通过插件配置覆盖。 */
15
+ const DEFAULT_RUNTIME = '0.1.3-alpha.1'
16
+ const REGISTRY = 'https://registry.npmjs.org'
17
+ const FETCH_TIMEOUT_MS = 15_000
18
+
19
+ export const name = 'tokens-plugin-check'
20
+ export const inject = ['tools']
21
+
22
+ async function fetchManifest(packageName, version) {
23
+ const controller = new AbortController()
24
+ const timer = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS)
25
+ try {
26
+ const response = await fetch(
27
+ `${REGISTRY}/${encodeURIComponent(packageName)}/${encodeURIComponent(version)}`,
28
+ { signal: controller.signal, headers: { accept: 'application/json' } },
29
+ )
30
+ if (!response.ok) {
31
+ throw new Error(response.status === 404 ? `npm 上未找到 ${packageName}@${version}` : `npm 查询失败(HTTP ${response.status})`)
32
+ }
33
+ return await response.json()
34
+ } finally {
35
+ clearTimeout(timer)
36
+ }
37
+ }
38
+
39
+ /**
40
+ * @param {import('@deepseek-ai/cordis').Context} ctx
41
+ * @param {{ runtime?: string }} [config]
42
+ */
43
+ export function apply(ctx, config = {}) {
44
+ const runtime = typeof config.runtime === 'string' && config.runtime !== '' ? config.runtime : DEFAULT_RUNTIME
45
+ ctx.tools.register(defineTool({
46
+ name: 'plugin_check',
47
+ description: 'Check whether a published DSH/TokensCowork plugin npm package is safe to install: '
48
+ + 'forbidden lifecycle scripts, dual-kernel core dependencies, missing dsh bundle patch, '
49
+ + 'and runtime/peer compatibility against the current DSH runtime. '
50
+ + 'Returns ok=false with per-rule findings when the plugin would break the host.',
51
+ parameters: {
52
+ package: {
53
+ type: 'string',
54
+ required: true,
55
+ description: 'npm package name, e.g. "@tokensapi/dsh-progressive-tools".',
56
+ },
57
+ version: {
58
+ type: 'string',
59
+ description: 'Exact version to check. Defaults to the latest dist-tag.',
60
+ },
61
+ },
62
+ output: {
63
+ schema: { type: 'json' },
64
+ render: (_args, value) => [{ type: 'text', text: JSON.stringify(value) }],
65
+ },
66
+ isConcurrencySafe: () => true,
67
+ async execute(args) {
68
+ const manifest = await fetchManifest(args.package, args.version ?? 'latest')
69
+ const { findings, inspectedPatch } = await checkPublishedPackage(manifest, runtime)
70
+ return {
71
+ package: manifest.name,
72
+ version: manifest.version,
73
+ runtime,
74
+ ok: !findings.some(finding => finding.level === 'error'),
75
+ inspectedPatch,
76
+ findings,
77
+ note: inspectedPatch
78
+ ? '以上为清单规则 + 包内补丁行检查;含隔离启动冒烟的完整体检请运行 npx @tokensapi/dsh-plugin-check --package <name@version>'
79
+ : '未读到包内补丁文件,补丁行未检查;请运行 npx @tokensapi/dsh-plugin-check --package <name@version> 做完整体检',
80
+ }
81
+ },
82
+ }))
83
+ }
@@ -1,144 +1,139 @@
1
- /* ============================================================
2
- * 阶段①:清单体检(纯静态,秒级,离线)
3
- * ============================================================
4
- * 每一条规则都对应一种已经真实发生过的"把宿主搞崩/搞坏"路径,
5
- * 规则说明写在 README;error 挡上架,warning 提示即将收紧或
6
- * 潜在错配。判定为纯函数,便于单测与市场门禁复用。
7
- * ============================================================ */
8
- import { existsSync, readFileSync, statSync } from 'node:fs'
9
- import { join, resolve } from 'node:path'
10
- import semver from 'semver'
11
- import { extractPatchRows } from './patch-rows.mjs'
12
-
13
- /** 安装即执行任意代码的脚本;市场受控安装的既有红线。 */
14
- const FORBIDDEN_LIFECYCLE = ['preinstall', 'install', 'postinstall', 'prepare']
15
-
16
- /**
17
- * 携带身份语义的核心包:cordis 上下文与 dsh 服务在宿主/插件各持一份时
18
- * Symbol 不等,注册对不上。schemastery、cosmokit 等纯工具库不在此列,
19
- * 插件可以正常依赖。
20
- */
21
- const CORE_SCOPE = '@deepseek-ai/'
22
- const isIdentityCore = (name) => name === '@deepseek-ai/cordis'
23
- || name.startsWith('@deepseek-ai/cordis-')
24
- || name.startsWith('@deepseek-ai/dsh')
25
-
26
- const SUSPICIOUS_FILE = /(\.env(\.|$)|id_rsa|\.pem$|\.p12$|\.key$)/u
27
-
28
- /**
29
- * 对一个插件目录执行全部清单规则。
30
- * @param {string} pluginDir - 插件根目录(含 package.json)。
31
- * @param {{ runtime?: string }} [options] - runtime 为目标 DSH 运行时
32
- * 版本(如 0.1.3-alpha.1);给出时才执行 engine/peer 与其的交集判定。
33
- * @returns {{ findings: Array<{level: 'error'|'warning', rule: string, message: string}>, manifest?: object, rows?: object[] }}
34
- */
35
- export function checkManifest(pluginDir, options = {}) {
36
- const findings = []
37
- const error = (rule, message) => findings.push({ level: 'error', rule, message })
38
- const warning = (rule, message) => findings.push({ level: 'warning', rule, message })
39
-
40
- const manifestPath = resolve(pluginDir, 'package.json')
41
- if (!existsSync(manifestPath)) {
42
- error('M1-manifest', 'package.json 不存在')
43
- return { findings }
44
- }
45
- let manifest
46
- try {
47
- manifest = JSON.parse(readFileSync(manifestPath, 'utf8'))
48
- } catch (cause) {
49
- error('M1-manifest', `package.json 不是合法 JSON: ${cause instanceof Error ? cause.message : String(cause)}`)
50
- return { findings }
51
- }
52
-
53
- if (typeof manifest.name !== 'string' || manifest.name === '') {
54
- error('M1-manifest', 'name 缺失')
55
- }
56
- if (typeof manifest.version !== 'string' || semver.valid(manifest.version) === null) {
57
- error('M1-manifest', 'version 不是合法 SemVer')
58
- } else if (semver.prerelease(manifest.version) !== null) {
59
- warning('M1-manifest', `version ${manifest.version} 是预发布版;市场上架要求精确稳定版`)
60
- }
61
-
62
- const scripts = manifest.scripts ?? {}
63
- for (const script of FORBIDDEN_LIFECYCLE) {
64
- if (typeof scripts[script] === 'string') {
65
- error('M2-lifecycle', `禁止 ${script} 生命周期脚本(安装即执行任意代码,市场受控安装会拒绝);构建请改用 prepack`)
66
- }
67
- }
68
-
69
- const dependencies = manifest.dependencies ?? {}
70
- for (const name of Object.keys(dependencies)) {
71
- if (isIdentityCore(name)) {
72
- error('M3-core-peer', `${name} 出现在 dependencies;核心运行时必须声明为 peerDependencies,否则宿主与插件各持一份实例,服务注册无法对上,典型症状是整个 Cowork 启动失败`)
73
- }
74
- }
75
-
76
- const dsh = manifest.dsh ?? {}
77
- const bundle = dsh.bundle ?? {}
78
- let rows
79
- if (typeof bundle.patch !== 'string' || bundle.patch === '') {
80
- error('M4-bundle', 'dsh.bundle.patch 缺失;宿主靠它把插件挂进加载树')
81
- } else {
82
- const patchPath = resolve(pluginDir, bundle.patch)
83
- if (!existsSync(patchPath)) {
84
- error('M4-bundle', `dsh.bundle.patch 指向的文件不存在: ${bundle.patch}`)
85
- } else {
86
- const extracted = extractPatchRows(readFileSync(patchPath, 'utf8'))
87
- for (const problem of extracted.problems) error('M4-bundle', `补丁行不合法: ${problem}`)
88
- rows = extracted.rows
89
- if (rows.length === 0 && extracted.problems.length === 0) {
90
- error('M4-bundle', '补丁未声明任何插件行')
91
- }
92
- const packageName = typeof manifest.name === 'string' ? manifest.name : ''
93
- for (const row of rows ?? []) {
94
- if (packageName !== '' && row.name !== packageName && !row.name.startsWith(`${packageName}/`)) {
95
- error('M5-row-scope', `补丁行 "${row.id ?? row.name}" 指向 ${row.name},超出本包命名空间(${packageName});插件只能挂载自己的入口`)
96
- }
97
- }
98
- }
99
- }
100
-
101
- const engine = typeof dsh.engine === 'string' ? dsh.engine : undefined
102
- if (engine === undefined) {
103
- warning('M6-engine', 'dsh.engine 未声明(目标 DSH 运行时 SemVer 范围,如 ">=0.1.3-alpha.1 <0.2");该字段将成为上架必填')
104
- } else if (semver.validRange(engine, { includePrerelease: true }) === null) {
105
- error('M6-engine', `dsh.engine 不是合法 SemVer 范围: ${engine}`)
106
- } else if (options.runtime !== undefined
107
- && !semver.satisfies(options.runtime, engine, { includePrerelease: true })) {
108
- error('M6-engine', `dsh.engine "${engine}" 不覆盖目标运行时 ${options.runtime}`)
109
- }
110
-
111
- const peers = manifest.peerDependencies ?? {}
112
- if (options.runtime !== undefined) {
113
- for (const [name, range] of Object.entries(peers)) {
114
- if (!name.startsWith(`${CORE_SCOPE}dsh`)) continue
115
- if (semver.validRange(range, { includePrerelease: true }) === null) {
116
- error('M7-peer-range', `peer ${name} 的范围不合法: ${range}`)
117
- } else if (!semver.satisfies(options.runtime, range, { includePrerelease: true })) {
118
- warning('M7-peer-range', `peer ${name}@"${range}" 不含目标运行时 ${options.runtime};宿主升级后此插件可能在运行时断裂(接口错配类故障)`)
119
- }
120
- }
121
- }
122
-
123
- if (manifest.engines?.node === undefined) {
124
- warning('M8-node-engines', 'engines.node 未声明;宿主运行在 Node 22+')
125
- }
126
- if (manifest.license === undefined) {
127
- warning('M9-license', 'license 未声明')
128
- }
129
-
130
- const filesList = Array.isArray(manifest.files) ? manifest.files : []
131
- for (const entry of filesList) {
132
- if (typeof entry === 'string' && SUSPICIOUS_FILE.test(entry)) {
133
- error('M10-secrets', `files 白名单包含疑似凭据文件: ${entry}`)
134
- }
135
- }
136
- for (const candidate of ['.env', 'id_rsa']) {
137
- const candidatePath = join(pluginDir, candidate)
138
- if (existsSync(candidatePath) && statSync(candidatePath).isFile() && filesList.length === 0) {
139
- warning('M10-secrets', `目录含 ${candidate} 且未用 files 白名单限定发布内容,npm pack 可能把它带上`)
140
- }
141
- }
142
-
143
- return { findings, manifest, rows }
144
- }
1
+ /* ============================================================
2
+ * 阶段①:清单体检(纯静态,秒级,离线)
3
+ * ============================================================
4
+ * 每一条规则都对应一种已经真实发生过的"把宿主搞崩/搞坏"路径,
5
+ * 规则说明写在 README;error 挡上架,warning 提示即将收紧或
6
+ * 潜在错配。判定为纯函数,便于单测与市场门禁复用。
7
+ * ============================================================ */
8
+ import { existsSync, readFileSync, statSync } from 'node:fs'
9
+ import { join, resolve } from 'node:path'
10
+ import semver from 'semver'
11
+ import { checkPatchContent } from './patch-rows.mjs'
12
+
13
+ /** 安装即执行任意代码的脚本;市场受控安装的既有红线。 */
14
+ const FORBIDDEN_LIFECYCLE = ['preinstall', 'install', 'postinstall', 'prepare']
15
+
16
+ /**
17
+ * 携带身份语义的核心包:cordis 上下文与 dsh 服务在宿主/插件各持一份时
18
+ * Symbol 不等,注册对不上。schemastery、cosmokit 等纯工具库不在此列,
19
+ * 插件可以正常依赖。
20
+ */
21
+ const CORE_SCOPE = '@deepseek-ai/'
22
+ const isIdentityCore = (name) => name === '@deepseek-ai/cordis'
23
+ || name.startsWith('@deepseek-ai/cordis-')
24
+ || name.startsWith('@deepseek-ai/dsh')
25
+
26
+ const SUSPICIOUS_FILE = /(\.env(\.|$)|id_rsa|\.pem$|\.p12$|\.key$)/u
27
+
28
+ /**
29
+ * 对一个插件目录执行全部清单规则。
30
+ * @param {string} pluginDir - 插件根目录(含 package.json)。
31
+ * @param {{ runtime?: string }} [options] - runtime 为目标 DSH 运行时
32
+ * 版本(如 0.1.3-alpha.1);给出时才执行 engine/peer 与其的交集判定。
33
+ * @returns {{ findings: Array<{level: 'error'|'warning', rule: string, message: string}>, manifest?: object, rows?: object[] }}
34
+ */
35
+ export function checkManifest(pluginDir, options = {}) {
36
+ const findings = []
37
+ const error = (rule, message) => findings.push({ level: 'error', rule, message })
38
+ const warning = (rule, message) => findings.push({ level: 'warning', rule, message })
39
+
40
+ const manifestPath = resolve(pluginDir, 'package.json')
41
+ if (!existsSync(manifestPath)) {
42
+ error('M1-manifest', 'package.json 不存在')
43
+ return { findings }
44
+ }
45
+ let manifest
46
+ try {
47
+ manifest = JSON.parse(readFileSync(manifestPath, 'utf8'))
48
+ } catch (cause) {
49
+ error('M1-manifest', `package.json 不是合法 JSON: ${cause instanceof Error ? cause.message : String(cause)}`)
50
+ return { findings }
51
+ }
52
+
53
+ if (typeof manifest.name !== 'string' || manifest.name === '') {
54
+ error('M1-manifest', 'name 缺失')
55
+ }
56
+ if (typeof manifest.version !== 'string' || semver.valid(manifest.version) === null) {
57
+ error('M1-manifest', 'version 不是合法 SemVer')
58
+ } else if (semver.prerelease(manifest.version) !== null) {
59
+ warning('M1-manifest', `version ${manifest.version} 是预发布版;市场上架要求精确稳定版`)
60
+ }
61
+
62
+ const scripts = manifest.scripts ?? {}
63
+ for (const script of FORBIDDEN_LIFECYCLE) {
64
+ if (typeof scripts[script] === 'string') {
65
+ error('M2-lifecycle', `禁止 ${script} 生命周期脚本(安装即执行任意代码,市场受控安装会拒绝);构建请改用 prepack`)
66
+ }
67
+ }
68
+
69
+ const dependencies = manifest.dependencies ?? {}
70
+ for (const name of Object.keys(dependencies)) {
71
+ if (isIdentityCore(name)) {
72
+ error('M3-core-peer', `${name} 出现在 dependencies;核心运行时必须声明为 peerDependencies,否则宿主与插件各持一份实例,服务注册无法对上,典型症状是整个 Cowork 启动失败`)
73
+ }
74
+ }
75
+
76
+ const dsh = manifest.dsh ?? {}
77
+ const bundle = dsh.bundle ?? {}
78
+ let rows
79
+ if (typeof bundle.patch !== 'string' || bundle.patch === '') {
80
+ error('M4-bundle', 'dsh.bundle.patch 缺失;宿主靠它把插件挂进加载树')
81
+ } else {
82
+ const patchPath = resolve(pluginDir, bundle.patch)
83
+ if (!existsSync(patchPath)) {
84
+ error('M4-bundle', `dsh.bundle.patch 指向的文件不存在: ${bundle.patch}`)
85
+ } else {
86
+ // M4/M5 与会话内体检共用同一份实现,避免两条路径判定漂移。
87
+ const checked = checkPatchContent(
88
+ typeof manifest.name === 'string' ? manifest.name : '',
89
+ readFileSync(patchPath, 'utf8'),
90
+ )
91
+ findings.push(...checked.findings)
92
+ rows = checked.rows
93
+ }
94
+ }
95
+
96
+ const engine = typeof dsh.engine === 'string' ? dsh.engine : undefined
97
+ if (engine === undefined) {
98
+ warning('M6-engine', 'dsh.engine 未声明(目标 DSH 运行时 SemVer 范围,如 ">=0.1.3-alpha.1 <0.2");该字段将成为上架必填')
99
+ } else if (semver.validRange(engine, { includePrerelease: true }) === null) {
100
+ error('M6-engine', `dsh.engine 不是合法 SemVer 范围: ${engine}`)
101
+ } else if (options.runtime !== undefined
102
+ && !semver.satisfies(options.runtime, engine, { includePrerelease: true })) {
103
+ error('M6-engine', `dsh.engine "${engine}" 不覆盖目标运行时 ${options.runtime}`)
104
+ }
105
+
106
+ const peers = manifest.peerDependencies ?? {}
107
+ if (options.runtime !== undefined) {
108
+ for (const [name, range] of Object.entries(peers)) {
109
+ if (!name.startsWith(`${CORE_SCOPE}dsh`)) continue
110
+ if (semver.validRange(range, { includePrerelease: true }) === null) {
111
+ error('M7-peer-range', `peer ${name} 的范围不合法: ${range}`)
112
+ } else if (!semver.satisfies(options.runtime, range, { includePrerelease: true })) {
113
+ warning('M7-peer-range', `peer ${name}@"${range}" 不含目标运行时 ${options.runtime};宿主升级后此插件可能在运行时断裂(接口错配类故障)`)
114
+ }
115
+ }
116
+ }
117
+
118
+ if (manifest.engines?.node === undefined) {
119
+ warning('M8-node-engines', 'engines.node 未声明;宿主运行在 Node 22+')
120
+ }
121
+ if (manifest.license === undefined) {
122
+ warning('M9-license', 'license 未声明')
123
+ }
124
+
125
+ const filesList = Array.isArray(manifest.files) ? manifest.files : []
126
+ for (const entry of filesList) {
127
+ if (typeof entry === 'string' && SUSPICIOUS_FILE.test(entry)) {
128
+ error('M10-secrets', `files 白名单包含疑似凭据文件: ${entry}`)
129
+ }
130
+ }
131
+ for (const candidate of ['.env', 'id_rsa']) {
132
+ const candidatePath = join(pluginDir, candidate)
133
+ if (existsSync(candidatePath) && statSync(candidatePath).isFile() && filesList.length === 0) {
134
+ warning('M10-secrets', `目录含 ${candidate} 且未用 files 白名单限定发布内容,npm pack 可能把它带上`)
135
+ }
136
+ }
137
+
138
+ return { findings, manifest, rows }
139
+ }