feat(invariants): implement package runtime checks

This commit is contained in:
Tianyi Cui
2026-07-20 00:38:37 +08:00
parent 36e99e737b
commit 941b0411d8
125 changed files with 2317 additions and 1161 deletions

View File

@@ -1,14 +1,10 @@
/**
* Generated invariant ownership companion for `@deepseek-ai/dsh-code-runtime-worker`.
* Replace this file with package-owned checks while preserving its registration.
*
* @generated scripts/gen-package-invariants.ts
* Package-owned runtime contract checks for `@deepseek-ai/dsh-code-runtime-worker`.
* @module @deepseek-ai/dsh-code-runtime-worker/invariant
*/
/* jscpd:ignore-start */
import type { Context } from 'cordis'
import type { InvariantInstaller } from '@deepseek-ai/dsh-invariants'
import { observePluginInvariant, type InvariantInstaller } from '@deepseek-ai/dsh-invariants'
const PACKAGE_NAME = '@deepseek-ai/dsh-code-runtime-worker'
@@ -17,8 +13,19 @@ export const name = 'code-runtime-worker-invariant'
/** Services required before the companion can register. */
export const inject = ['invariants']
/** Reserve this package's invariant ownership until it adds relational checks. */
const install: InvariantInstaller = () => {}
/** Install checks for this package's active plugin fibers. */
const install: InvariantInstaller = (ctx, fail) => {
observePluginInvariant(ctx, fail, {
name: 'WorkerCodeRuntime',
effects: [
'ctx.provide("codeRuntime")',
'worker code-runtime teardown',
],
services: [
'codeRuntime',
],
})
}
/**
* Register this package's invariant companion.
@@ -27,4 +34,3 @@ const install: InvariantInstaller = () => {}
*/
export const apply = (ctx: Context): Promise<() => void> =>
Promise.resolve(ctx.invariants.register(PACKAGE_NAME, install))
/* jscpd:ignore-end */

View File

@@ -1,14 +1,7 @@
/**
* Generated invariant ownership companion for `@deepseek-ai/dsh-code-runtime`.
* Replace this file with package-owned checks while preserving its registration.
*
* @generated scripts/gen-package-invariants.ts
* @module @deepseek-ai/dsh-code-runtime/invariant
*/
/** Package-owned runtime contract checks for `@deepseek-ai/dsh-code-runtime`. @module @deepseek-ai/dsh-code-runtime/invariant */
/* jscpd:ignore-start */
import type { Context } from 'cordis'
import type { InvariantInstaller } from '@deepseek-ai/dsh-invariants'
import { observeServiceInvariant, serviceShapeViolation, type InvariantInstaller } from '@deepseek-ai/dsh-invariants'
const PACKAGE_NAME = '@deepseek-ai/dsh-code-runtime'
@@ -17,8 +10,20 @@ export const name = 'code-runtime-invariant'
/** Services required before the companion can register. */
export const inject = ['invariants']
/** Reserve this package's invariant ownership until it adds relational checks. */
const install: InvariantInstaller = () => {}
/** Validate every implementation bound to this package's service seam. */
const install: InvariantInstaller = (ctx, fail) => {
observeServiceInvariant(ctx, fail, 'codeRuntime', (value) => {
const violation = serviceShapeViolation(value, {
methods: ['run'],
stringProperties: ['language', 'isolation'],
})
if (violation !== undefined) return violation
const service = value as { language: string; isolation: string }
return /^[a-z][a-z0-9-]*$/.test(service.language) && /^[a-z][a-z0-9-]*$/.test(service.isolation)
? undefined
: 'code runtime language and isolation must be lowercase identifiers'
})
}
/**
* Register this package's invariant companion.
@@ -27,4 +32,3 @@ const install: InvariantInstaller = () => {}
*/
export const apply = (ctx: Context): Promise<() => void> =>
Promise.resolve(ctx.invariants.register(PACKAGE_NAME, install))
/* jscpd:ignore-end */

View File

@@ -84,4 +84,18 @@ describe('CodeRuntime service seam', () => {
const { ctx } = await setup()
await expect(ctx.plugin(StubRuntime)).rejects.toThrow(/registered/)
})
it.each([
[{ language: 'typescript', isolation: 'worker' }, /must expose method "run"/],
[{ language: 'TypeScript', isolation: 'worker', run() {} }, /must be lowercase identifiers/],
])('rejects an invalid runtime implementation through the package invariant', async (value, message) => {
const ctx = new Context()
const invalidRuntime = {
name: 'invalid-code-runtime',
apply(child: Context) {
child.provide('codeRuntime', value as unknown as CodeRuntime)
},
}
await expect(ctx.plugin(invalidRuntime)).rejects.toThrow(message)
})
})