fix(invariants): assert runtime relationships, not API shapes

This commit is contained in:
Tianyi Cui
2026-07-20 19:34:19 +08:00
parent 1254c07025
commit 1145ee5fc3
124 changed files with 2923 additions and 2334 deletions
+13 -9
View File
@@ -1,21 +1,24 @@
/** Package-owned runtime contract checks for `@deepseek-ai/dsh-sandbox`. @module @deepseek-ai/dsh-sandbox/invariant */
/**
* Package-owned invariant companion for `@deepseek-ai/dsh-sandbox`.
* @module @deepseek-ai/dsh-sandbox/invariant
*/
/* jscpd:ignore-start */
import type { Context } from 'cordis'
import { observeServiceInvariant, serviceShapeViolation, type InvariantInstaller } from '@deepseek-ai/dsh-invariants'
import type { InvariantInstaller } from '@deepseek-ai/dsh-invariants'
const PACKAGE_NAME = '@deepseek-ai/dsh-sandbox'
/** Cordis companion plugin name. */
export const name = 'sandbox-invariant'
/** Services required before the companion can register. */
/** Service required before the companion can reserve package ownership. */
export const inject = ['invariants']
/** Validate every implementation bound to this package's service seam. */
const install: InvariantInstaller = (ctx, fail) => {
observeServiceInvariant(ctx, fail, 'sandbox', value => serviceShapeViolation(value, {
methods: ['confine'],
}))
}
/**
* No runtime invariant: this package exposes no independent event sequence or mutable data relation
* beyond contracts enforced at its owning seam.
*/
const install: InvariantInstaller = () => {}
/**
* Register this package's invariant companion.
@@ -24,3 +27,4 @@ const install: InvariantInstaller = (ctx, fail) => {
*/
export const apply = (ctx: Context): Promise<() => void> =>
Promise.resolve(ctx.invariants.register(PACKAGE_NAME, install))
/* jscpd:ignore-end */
@@ -1,43 +0,0 @@
import { describe, expect, it } from 'vitest'
import { Context } from 'cordis'
import { InvariantError } from '@deepseek-ai/dsh-invariants'
import { SandboxProvider } from '@deepseek-ai/dsh-sandbox'
import type { ConfinedArgv, SandboxPolicy } from '@deepseek-ai/dsh-sandbox'
class StubSandboxProvider extends SandboxProvider {
confine(argv: readonly string[], _policy: SandboxPolicy): ConfinedArgv {
return {
argv: [...argv],
enforcement: 'full',
denialSignatures: [],
runnerFailureSignatures: [],
}
}
}
describe('sandbox package invariant', () => {
it('accepts a provider that exposes the confinement seam', async () => {
const ctx = new Context()
await ctx.plugin(StubSandboxProvider)
expect(ctx.sandbox).toBeInstanceOf(StubSandboxProvider)
})
it('rejects a service binding without confine()', async () => {
const ctx = new Context()
const invalidSandbox = {
name: 'invalid-sandbox',
apply(child: Context) {
child.provide('sandbox', {} as SandboxProvider)
},
}
let caught: unknown
try {
await ctx.plugin(invalidSandbox)
} catch (error) {
caught = error
}
expect(caught).toBeInstanceOf(InvariantError)
expect(caught).toHaveProperty('packageName', '@deepseek-ai/dsh-sandbox')
expect((caught as Error).message).toMatch(/must expose method "confine"/)
})
})