feat(shell-env): add ShellEnvError for invalid contributions
A contributor that returns an undeclared key or a non-string value now fails the shell call with ShellEnvError SHELL_ENV_CONTRIBUTION_INVALID instead of a plain Error, with the same message. Refs #7869
This commit is contained in:
1 parent
3538757cbe
commit
818b603ed4
7 files changed
+42
-7
No files matched your search
@@ -21,8 +21,8 @@
|
||||
en: bab0b75fb967d41a
|
||||
zh: 8f636d9965d01ad1
|
||||
/deepseek-ai-dsh-shell-env/use-this-package/adding-your-own-environment-facts:
|
||||
en: 21ef223626e35905
|
||||
zh: 5c468055099b0135
|
||||
en: 14da34f853d089be
|
||||
zh: 5ad3942aa5321893
|
||||
/deepseek-ai-dsh-shell-env/use-this-package/choosing-the-harness-home:
|
||||
en: 0041438f2b7f7e4f
|
||||
zh: 93fa165b07adcdf4
|
||||
|
||||
@@ -50,7 +50,7 @@ export function apply(ctx: Context): void {
|
||||
}
|
||||
```
|
||||
|
||||
Contributors must declare every key they return; returning an undeclared or non-string value fails the call. Registration is disposed with the registering plugin, so hot-reloading a plugin removes its facts.
|
||||
Contributors must declare every key they return; returning an undeclared or non-string value fails the call with `ShellEnvError` (`SHELL_ENV_CONTRIBUTION_INVALID`). Registration is disposed with the registering plugin, so hot-reloading a plugin removes its facts.
|
||||
|
||||
### Choosing the Harness home
|
||||
|
||||
|
||||
@@ -50,7 +50,7 @@ export function apply(ctx: Context): void {
|
||||
}
|
||||
```
|
||||
|
||||
contributor 必须声明它返回的每个键;返回未声明或非字符串的值会让该次调用失败。注册随注册插件的释放而释放,因此热重载插件会移除它的事实。
|
||||
contributor 必须声明它返回的每个键;返回未声明或非字符串的值会让该次调用以 `ShellEnvError`(`SHELL_ENV_CONTRIBUTION_INVALID`)失败。注册随注册插件的释放而释放,因此热重载插件会移除它的事实。
|
||||
|
||||
### 选择 Harness 主目录
|
||||
|
||||
|
||||
@@ -36,7 +36,8 @@
|
||||
"@deepseek-ai/dsh-app-boot": "workspace:*",
|
||||
"@deepseek-ai/dsh-home-paths": "workspace:*",
|
||||
"@deepseek-ai/dsh-shell": "workspace:*",
|
||||
"@deepseek-ai/dsh-tools": "workspace:*"
|
||||
"@deepseek-ai/dsh-tools": "workspace:*",
|
||||
"@deepseek-ai/dsh-llm": "workspace:*"
|
||||
},
|
||||
"dependencies": {
|
||||
"@deepseek-ai/schemastery": "workspace:~"
|
||||
|
||||
@@ -14,6 +14,7 @@ import z from '@deepseek-ai/schemastery'
|
||||
import { DSH_ENV_PREFIX } from '@deepseek-ai/dsh-shell'
|
||||
import type { DshEnvironment, DshEnvironmentKey } from '@deepseek-ai/dsh-shell'
|
||||
import { DSH_HOME_ENV, resolveDshHome } from '@deepseek-ai/dsh-home-paths'
|
||||
import { HarnessError } from '@deepseek-ai/dsh-llm'
|
||||
import type { ToolExecution } from '@deepseek-ai/dsh-tools'
|
||||
// Declares `Context.profileContext`, the launcher-provided profile the built-ins read.
|
||||
import type {} from '@deepseek-ai/dsh-app-boot'
|
||||
@@ -24,6 +25,31 @@ declare module '@deepseek-ai/cordis' {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Every {@link ShellEnvError} code:
|
||||
* - `SHELL_ENV_CONTRIBUTION_INVALID` — a registered contributor returned an undeclared key or a non-string value; the
|
||||
* shell command does not run, and the contributing plugin must be fixed.
|
||||
*/
|
||||
export const SHELL_ENV_ERROR_CODES = ['SHELL_ENV_CONTRIBUTION_INVALID'] as const
|
||||
|
||||
/** One code from {@link SHELL_ENV_ERROR_CODES}. */
|
||||
export type ShellEnvErrorCode = typeof SHELL_ENV_ERROR_CODES[number]
|
||||
|
||||
/** Shell environment failure carrying a stable {@link ShellEnvErrorCode}. */
|
||||
export class ShellEnvError extends HarnessError {
|
||||
override readonly code: ShellEnvErrorCode
|
||||
|
||||
/**
|
||||
* @param message - names the contributor and the offending key.
|
||||
* @param code - the failure class consumers route on.
|
||||
*/
|
||||
constructor(message: string, code: ShellEnvErrorCode) {
|
||||
super(message, code)
|
||||
this.name = 'ShellEnvError'
|
||||
this.code = code
|
||||
}
|
||||
}
|
||||
|
||||
export const name = 'shell-env'
|
||||
export const inject: string[] = []
|
||||
|
||||
@@ -182,10 +208,13 @@ export class ShellEnvRegistry extends Service {
|
||||
for (const [rawKey, value] of Object.entries(resolved)) {
|
||||
const key = rawKey as DshEnvironmentKey
|
||||
if (!Object.hasOwn(contributor.variables, key)) {
|
||||
throw new Error(`bash env contributor "${contributor.name}" returned undeclared key "${key}"`)
|
||||
throw new ShellEnvError(`bash env contributor "${contributor.name}" returned undeclared key "${key}"`, 'SHELL_ENV_CONTRIBUTION_INVALID')
|
||||
}
|
||||
if (typeof value !== 'string') {
|
||||
throw new Error(`bash env contributor "${contributor.name}" returned a non-string value for "${key}"`)
|
||||
throw new ShellEnvError(
|
||||
`bash env contributor "${contributor.name}" returned a non-string value for "${key}"`,
|
||||
'SHELL_ENV_CONTRIBUTION_INVALID',
|
||||
)
|
||||
}
|
||||
values[key] = value
|
||||
}
|
||||
|
||||
@@ -192,6 +192,7 @@ describe('ShellEnvRegistry', () => {
|
||||
})
|
||||
|
||||
expect(() => registry.collect(execution())).toThrow(/drifted-provider.*DSH_UNDECLARED/)
|
||||
expect(() => registry.collect(execution())).toThrow(expect.objectContaining({ name: 'ShellEnvError', code: 'SHELL_ENV_CONTRIBUTION_INVALID' }))
|
||||
})
|
||||
|
||||
it('rejects non-string values returned by a contributor', () => {
|
||||
@@ -203,6 +204,7 @@ describe('ShellEnvRegistry', () => {
|
||||
})
|
||||
|
||||
expect(() => registry.collect(execution())).toThrow(/wrong-value-type.*non-string.*DSH_STRING/)
|
||||
expect(() => registry.collect(execution())).toThrow(expect.objectContaining({ name: 'ShellEnvError', code: 'SHELL_ENV_CONTRIBUTION_INVALID' }))
|
||||
})
|
||||
|
||||
it('removes an effect-scoped contributor when its plugin is disposed', async () => {
|
||||
|
||||
@@ -28,6 +28,9 @@
|
||||
},
|
||||
{
|
||||
"path": "../../boot/app-boot"
|
||||
},
|
||||
{
|
||||
"path": "../../llm/llm"
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in new issue
Block a user