fix(upstream-skills): validate prefixes at load and refresh skills another process published
A skillsUrls value that does not parse as a URL now fails the configuration instead of aborting the download loop, skillsTimeoutMs must be whole milliseconds, and each failed prefix is logged in full so prefixes on one host stay distinguishable. A download that fails after another process published the directory invalidates the skill list, so the entry is listed. The timeout test gives the mirror three seconds.
This commit is contained in:
1 parent
15622f9515
commit
c3d5b51743
6 files changed
+58
-26
No files matched your search
@@ -24,17 +24,17 @@
|
||||
en: c3e1b60c8e4e023a
|
||||
zh: e51792ead0b00e7f
|
||||
/deepseek-ai-dsh-upstream-skills/use-this-package/kept-files-and-verification:
|
||||
en: 7614b61ddd183abf
|
||||
zh: 1037e5fb77d3621f
|
||||
en: 49a8b6fe1e1df41a
|
||||
zh: 4cff59da21ae3dcb
|
||||
/deepseek-ai-dsh-upstream-skills/use-this-package/pinning-a-commit:
|
||||
en: 5c7edfe292e3ed4e
|
||||
zh: 4f8fbac5a4ac9a71
|
||||
/deepseek-ai-dsh-upstream-skills/use-this-package/shared-config:
|
||||
en: 3f3a4be4530b6314
|
||||
zh: 1cca96a68c8ae41e
|
||||
en: 590e7c5cc0c2ceaf
|
||||
zh: 09df0db0fc260a7e
|
||||
/deepseek-ai-dsh-upstream-skills/understand-the-implementation:
|
||||
en: bd132294ce3c09ab
|
||||
zh: b5eca377dad8d48c
|
||||
en: 97bd13a3bb1c31d9
|
||||
zh: cb827ed3fb450148
|
||||
/deepseek-ai-dsh-upstream-skills/further-exploration:
|
||||
en: 60965f68389a4cad
|
||||
zh: dc8e251f38301b9f
|
||||
|
||||
@@ -64,7 +64,7 @@ const skills = registerUpstreamSkills(ctx, {
|
||||
|
||||
### Kept files and verification
|
||||
|
||||
The library downloads `<prefix>/<owner>/<repository>/tar.gz/<commit>` from each `skillsUrls` prefix in order. A prefix that answers with an error status or no body, does not deliver the whole archive within `skillsTimeoutMs`, sends a malformed archive, or sends files that differ from the pin is logged with its origin, and the next prefix is tried; disposing the plugin stops at once. The library keeps every regular file under `directory` except the paths in `exclude`, plus `license`, at their repository paths. It refuses an archive with an empty, `.`, or `..` path segment or a backslash in any entry, entries outside its one top-level directory, a kept path that is a link or appears twice, kept files above `bytes`, or no license file. On Windows, node-tar turns backslashes into slashes before the library reads a path, so the segments they separate are checked instead. The digest hashes each kept file's path, byte length, and content in path order, because GitHub keeps the files of a commit archive stable but not its compressed bytes. Verified files are written to a sibling staging directory, which is then renamed to `skills.directory`; a directory that another process, such as Desktop beside Web, published first is kept.
|
||||
The library downloads `<prefix>/<owner>/<repository>/tar.gz/<commit>` from each `skillsUrls` prefix in order. A prefix that answers with an error status or no body, does not deliver the whole archive within `skillsTimeoutMs`, sends a malformed archive, or sends files that differ from the pin is logged with the prefix, and the next prefix is tried; disposing the plugin stops at once. The library keeps every regular file under `directory` except the paths in `exclude`, plus `license`, at their repository paths. It refuses an archive with an empty, `.`, or `..` path segment or a backslash in any entry, entries outside its one top-level directory, a kept path that is a link or appears twice, kept files above `bytes`, or no license file. On Windows, node-tar turns backslashes into slashes before the library reads a path, so the segments they separate are checked instead. The digest hashes each kept file's path, byte length, and content in path order, because GitHub keeps the files of a commit archive stable but not its compressed bytes. Verified files are written to a sibling staging directory, which is then renamed to `skills.directory`; a directory that another process, such as Desktop beside Web, published first is kept.
|
||||
|
||||
### Pinning a commit
|
||||
|
||||
@@ -75,8 +75,8 @@ The library downloads `<prefix>/<owner>/<repository>/tar.gz/<commit>` from each
|
||||
| Field | Default | Meaning |
|
||||
|---|---|---|
|
||||
| `skillsDir` | none | Directory holding the kept files at their repository paths; when set, nothing is downloaded, and a path that is not a directory fails the load |
|
||||
| `skillsUrls` | `['https://codeload.github.com']` | HTTPS archive download prefixes, without a query, fragment, or credentials, tried in order |
|
||||
| `skillsTimeoutMs` | `120000` | How long one prefix may take to deliver the whole archive before the next is tried |
|
||||
| `skillsUrls` | `['https://codeload.github.com']` | HTTPS archive download prefixes, without a query, fragment, or credentials, tried in order; a value that does not parse as a URL fails the configuration |
|
||||
| `skillsTimeoutMs` | `120000` | Whole milliseconds one prefix may take to deliver the whole archive before the next is tried |
|
||||
|
||||
-----
|
||||
|
||||
@@ -86,7 +86,7 @@ The library downloads `<prefix>/<owner>/<repository>/tar.gz/<commit>` from each
|
||||
<details>
|
||||
<summary>Implementation internals — click to expand</summary>
|
||||
|
||||
The provider lists the entry only while `skills.directory` exists, with source `bundled` at the bundled rank, so a project or user skill of the same name wins. It returns a complete, empty list before publishing, because an incomplete observation would make the whole skill catalog incomplete; publishing invalidates the catalog instead. One download runs at a time, and the provider registration's signal aborts it when the plugin is disposed. Kept files are held in memory, bounded by `bytes`, until they match the digest, so no unverified file reaches the disk; files outside the selection are read and dropped, so `skillsTimeoutMs` bounds the time spent on them.
|
||||
The provider lists the entry only while `skills.directory` exists, with source `bundled` at the bundled rank, so a project or user skill of the same name wins. It returns a complete, empty list before publishing, because an incomplete observation would make the whole skill catalog incomplete; publishing invalidates the catalog instead, as does a failed download that finds a directory another process published. One download runs at a time, and the provider registration's signal aborts it when the plugin is disposed. Kept files are held in memory, bounded by `bytes`, until they match the digest, so no unverified file reaches the disk; files outside the selection are read and dropped, so `skillsTimeoutMs` bounds the time spent on them.
|
||||
|
||||
| File | Role |
|
||||
|---|---|
|
||||
|
||||
@@ -64,7 +64,7 @@ const skills = registerUpstreamSkills(ctx, {
|
||||
|
||||
### 保留的文件与校验
|
||||
|
||||
库按顺序从每个 `skillsUrls` 前缀下载 `<前缀>/<owner>/<repository>/tar.gz/<commit>`。某个前缀返回错误状态或空响应体、没有在 `skillsTimeoutMs` 内送达完整归档、发来损坏的归档,或发来的文件与固定信息不符时,记录它的来源并尝试下一个;卸载插件则立即停止。库保留 `directory` 下除 `exclude` 列出的路径之外的所有普通文件,以及 `license`,并保持它们的仓库路径。以下归档会被拒绝:任一条目的路径含空段、`.`、`..` 或反斜杠;条目不在唯一的顶层目录下;保留的路径是链接或出现两次;保留的文件超过 `bytes`;没有许可证文件。Windows 上 node-tar 在库读取路径之前已把反斜杠转成 `/`,因此改为检查它分开的各段。摘要按路径顺序对每个保留文件的路径、字节长度和内容做哈希,因为 GitHub 保证提交归档里的文件不变,但不保证压缩后的字节不变。校验通过的文件先写入同级的暂存目录,再改名为 `skills.directory`;另一个进程(例如与 Web 同时运行的桌面端)先发布的目录会被保留。
|
||||
库按顺序从每个 `skillsUrls` 前缀下载 `<前缀>/<owner>/<repository>/tar.gz/<commit>`。某个前缀返回错误状态或空响应体、没有在 `skillsTimeoutMs` 内送达完整归档、发来损坏的归档,或发来的文件与固定信息不符时,记录该前缀并尝试下一个;卸载插件则立即停止。库保留 `directory` 下除 `exclude` 列出的路径之外的所有普通文件,以及 `license`,并保持它们的仓库路径。以下归档会被拒绝:任一条目的路径含空段、`.`、`..` 或反斜杠;条目不在唯一的顶层目录下;保留的路径是链接或出现两次;保留的文件超过 `bytes`;没有许可证文件。Windows 上 node-tar 在库读取路径之前已把反斜杠转成 `/`,因此改为检查它分开的各段。摘要按路径顺序对每个保留文件的路径、字节长度和内容做哈希,因为 GitHub 保证提交归档里的文件不变,但不保证压缩后的字节不变。校验通过的文件先写入同级的暂存目录,再改名为 `skills.directory`;另一个进程(例如与 Web 同时运行的桌面端)先发布的目录会被保留。
|
||||
|
||||
### 固定提交
|
||||
|
||||
@@ -75,8 +75,8 @@ const skills = registerUpstreamSkills(ctx, {
|
||||
| 字段 | 默认值 | 含义 |
|
||||
|---|---|---|
|
||||
| `skillsDir` | 无 | 按仓库路径存放保留文件的目录;设置后不下载,路径不是目录时加载失败 |
|
||||
| `skillsUrls` | `['https://codeload.github.com']` | 按顺序尝试的 HTTPS 归档下载前缀,不含查询串、片段或凭据 |
|
||||
| `skillsTimeoutMs` | `120000` | 一个前缀送达完整归档的时限,超时后尝试下一个 |
|
||||
| `skillsUrls` | `['https://codeload.github.com']` | 按顺序尝试的 HTTPS 归档下载前缀,不含查询串、片段或凭据;无法解析为 URL 的值使配置校验失败 |
|
||||
| `skillsTimeoutMs` | `120000` | 一个前缀送达完整归档的时限(整数毫秒),超时后尝试下一个 |
|
||||
|
||||
-----
|
||||
|
||||
@@ -86,7 +86,7 @@ const skills = registerUpstreamSkills(ctx, {
|
||||
<details>
|
||||
<summary>实现内部细节——点击展开</summary>
|
||||
|
||||
提供方只在 `skills.directory` 存在时列出入口技能,来源为 `bundled`、使用内置技能的优先级,因此同名的项目或用户技能优先。发布前它返回完整的空列表,因为不完整的结果会让整个技能目录变成不完整;发布后改为使目录失效。同一时间只运行一次下载,卸载插件时,提供方注册的信号会中止它。保留文件在与摘要一致之前只存在内存中,总量受 `bytes` 限制,因此未经校验的文件不会写入磁盘;选择范围之外的文件读取后即丢弃,花在它们上面的时间受 `skillsTimeoutMs` 限制。
|
||||
提供方只在 `skills.directory` 存在时列出入口技能,来源为 `bundled`、使用内置技能的优先级,因此同名的项目或用户技能优先。发布前它返回完整的空列表,因为不完整的结果会让整个技能目录变成不完整;发布后改为使目录失效;下载失败但发现另一个进程已发布目录时也使目录失效。同一时间只运行一次下载,卸载插件时,提供方注册的信号会中止它。保留文件在与摘要一致之前只存在内存中,总量受 `bytes` 限制,因此未经校验的文件不会写入磁盘;选择范围之外的文件读取后即丢弃,花在它们上面的时间受 `skillsTimeoutMs` 限制。
|
||||
|
||||
| 文件 | 职责 |
|
||||
|---|---|
|
||||
|
||||
@@ -166,7 +166,7 @@ export async function downloadSkillSet(
|
||||
} catch (error) {
|
||||
signal.throwIfAborted()
|
||||
failure = error
|
||||
warn(`downloading the upstream skills from ${new URL(url).origin} failed`, error)
|
||||
warn(`downloading the upstream skills from ${base} failed`, error)
|
||||
}
|
||||
}
|
||||
const at = `${set.repository} at ${set.commit}`
|
||||
|
||||
@@ -31,11 +31,19 @@ export interface UpstreamSkillsConfig {
|
||||
skillsTimeoutMs: number
|
||||
}
|
||||
|
||||
/** Accept a download prefix that parses as an HTTPS URL without credentials, a query, or a fragment. */
|
||||
function skillsUrl(value: string): string {
|
||||
if (!/^https:\/\/[^\s?#@]+$/u.test(value) || !URL.canParse(value)) {
|
||||
throw new Error(`skillsUrls: ${value} is not an HTTPS URL without credentials, a query, or a fragment`)
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
/** The shared fields' schema; `skillsUrls` defaults to GitHub's archive host. */
|
||||
export const UpstreamSkillsConfig: z<Partial<UpstreamSkillsConfig>, UpstreamSkillsConfig> = z.object({
|
||||
skillsDir: z.string().min(1),
|
||||
skillsUrls: z.array(z.string().pattern(/^https:\/\/[^\s?#@]+$/)).min(1).default(['https://codeload.github.com']),
|
||||
skillsTimeoutMs: z.number().min(1).max(MAX_TIMER_DELAY_MS).default(120_000),
|
||||
skillsUrls: z.array(z.transform(z.string(), skillsUrl)).min(1).default(['https://codeload.github.com']),
|
||||
skillsTimeoutMs: z.natural().min(1).max(MAX_TIMER_DELAY_MS).default(120_000),
|
||||
})
|
||||
|
||||
/** One plugin's packaged entry skill and the upstream files it indexes. */
|
||||
@@ -134,7 +142,12 @@ class UpstreamSkillProvider implements SkillProvider, UpstreamSkills {
|
||||
this.notify()
|
||||
void this.install(signal).then(
|
||||
() => { this.control.invalidate() },
|
||||
(error: unknown) => { if (!signal.aborted) this.logger.warn(`${this.name}: the upstream skills are not available`, error) },
|
||||
(error: unknown) => {
|
||||
if (signal.aborted) return
|
||||
// Another process may have published the directory while this download failed.
|
||||
if (this.installed()) this.control.invalidate()
|
||||
else this.logger.warn(`${this.name}: the upstream skills are not available`, error)
|
||||
},
|
||||
).finally(() => {
|
||||
this.running = false
|
||||
if (!signal.aborted) this.notify()
|
||||
|
||||
@@ -102,16 +102,20 @@ const listed = (directory: string) => [{
|
||||
}]
|
||||
|
||||
describe('UpstreamSkillsConfig', () => {
|
||||
it('defaults to GitHub with a two-minute limit and accepts only HTTPS prefixes without a query, fragment, or credentials', () => {
|
||||
it('defaults to GitHub with a two-minute limit and accepts only HTTPS URL prefixes without a query, fragment, or credentials', () => {
|
||||
expect(UpstreamSkillsConfig({})).toEqual({ skillsUrls: ['https://codeload.github.com'], skillsTimeoutMs: 120_000 })
|
||||
expect(UpstreamSkillsConfig({ skillsDir: '/opt/skills', skillsUrls: ['https://mirror.example/github'], skillsTimeoutMs: 1 }))
|
||||
.toEqual({ skillsDir: '/opt/skills', skillsUrls: ['https://mirror.example/github'], skillsTimeoutMs: 1 })
|
||||
for (const url of ['http://codeload.github.com', 'https://mirror.example/?a=1', 'https://mirror.example/#a', 'https://[email protected]']) {
|
||||
for (const url of [
|
||||
'http://codeload.github.com', 'https://mirror.example/?a=1', 'https://mirror.example/#a', 'https://[email protected]',
|
||||
'https://mirror.example:bad', 'https://[', 'https://:',
|
||||
]) {
|
||||
expect(() => UpstreamSkillsConfig({ skillsUrls: [url] })).toThrow()
|
||||
}
|
||||
expect(() => UpstreamSkillsConfig({ skillsUrls: [] })).toThrow()
|
||||
expect(() => UpstreamSkillsConfig({ skillsDir: '' })).toThrow()
|
||||
expect(() => UpstreamSkillsConfig({ skillsTimeoutMs: 0 })).toThrow()
|
||||
expect(() => UpstreamSkillsConfig({ skillsTimeoutMs: 1500.5 })).toThrow()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -173,21 +177,21 @@ describe('registerUpstreamSkills', () => {
|
||||
expect(host.requests).toEqual([archivePath(set)])
|
||||
})
|
||||
|
||||
it('tries the next skills URL after one fails and logs the failed origin', async () => {
|
||||
it('tries the next skills URL after one fails and logs the failed prefix', async () => {
|
||||
host.routes[archivePath(set, '/mirror')] = serve(repository())
|
||||
const skills = register({ skillsUrls: [`${host.base}/missing`, `${host.base}/mirror/`] })
|
||||
expect(await settled(skills)).toBe('installed')
|
||||
expect(await ctx.skills.list()).toEqual(listed(published()))
|
||||
expect(warnings()).toEqual([`vendor: downloading the upstream skills from ${host.base} failed`])
|
||||
expect(warnings()).toEqual([`vendor: downloading the upstream skills from ${host.base}/missing failed`])
|
||||
})
|
||||
|
||||
it('moves on from a skills URL that does not deliver the whole archive within the time limit', async () => {
|
||||
host.routes[archivePath(set, '/slow')] = (response) => { response.writeHead(200).write(repository().subarray(0, 64)) }
|
||||
host.routes[archivePath(set, '/mirror')] = serve(repository())
|
||||
// The limit applies to each URL, so it also bounds the mirror on a loaded machine.
|
||||
const skills = register({ skillsUrls: [`${host.base}/slow`, `${host.base}/mirror`], skillsTimeoutMs: 1_000 })
|
||||
const skills = register({ skillsUrls: [`${host.base}/slow`, `${host.base}/mirror`], skillsTimeoutMs: 3_000 })
|
||||
expect(await settled(skills)).toBe('installed')
|
||||
expect(warnings()).toEqual([`vendor: downloading the upstream skills from ${host.base} failed`])
|
||||
expect(warnings()).toEqual([`vendor: downloading the upstream skills from ${host.base}/slow failed`])
|
||||
// Depending on which stream observes the abort first, the time limit arrives as the TimeoutError or as its cause.
|
||||
const timedOut = (error: unknown) => error instanceof Error && [error.name, error.cause instanceof Error ? error.cause.name : ''].includes('TimeoutError')
|
||||
expect(logged()[0]?.error).toSatisfy(timedOut)
|
||||
@@ -199,8 +203,8 @@ describe('registerUpstreamSkills', () => {
|
||||
const skills = register({ skillsUrls: [`${host.base}/empty`, `${host.base}/changed`] })
|
||||
expect(await settled(skills)).toBe('failed')
|
||||
expect(warnings()).toEqual([
|
||||
`vendor: downloading the upstream skills from ${host.base} failed`,
|
||||
`vendor: downloading the upstream skills from ${host.base} failed`,
|
||||
`vendor: downloading the upstream skills from ${host.base}/empty failed`,
|
||||
`vendor: downloading the upstream skills from ${host.base}/changed failed`,
|
||||
'vendor: the upstream skills are not available',
|
||||
])
|
||||
expect(logged()[2]?.error).toMatchObject({
|
||||
@@ -250,6 +254,21 @@ describe('registerUpstreamSkills', () => {
|
||||
expect(await readdir(cache())).toEqual([`skills-${set.sha256.slice(0, 16)}`])
|
||||
})
|
||||
|
||||
it('lists the entry when another process publishes the directory while this download fails', async () => {
|
||||
const gate = new Promise<void>((resolve) => { release = resolve })
|
||||
host.routes[archivePath(set)] = async (response) => {
|
||||
await gate
|
||||
response.writeHead(500).end()
|
||||
}
|
||||
const skills = register()
|
||||
expect(await ctx.skills.list()).toEqual([])
|
||||
await mkdir(published(), { recursive: true })
|
||||
release()
|
||||
expect(await settled(skills)).toBe('installed')
|
||||
expect(await ctx.skills.list()).toEqual(listed(published()))
|
||||
expect(warnings()).toEqual([`vendor: downloading the upstream skills from ${host.base} failed`])
|
||||
})
|
||||
|
||||
it('reports a publish that fails without another process publishing', async () => {
|
||||
host.routes[archivePath(set)] = serve(repository())
|
||||
renameHook.before = async (from) => { await rm(from, { recursive: true }) }
|
||||
|
||||
Reference in new issue
Block a user