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 files changed
+2923 -2334

No files matched your search

+14 -8
View File
@@ -1,24 +1,30 @@
/** Package-owned runtime contract checks for `@deepseek-ai/dsh-bash`. @module @deepseek-ai/dsh-bash/invariant */
/** Package-owned session-event invariants for the bash seam. @module @deepseek-ai/dsh-bash/invariant */
import type { Context } from 'cordis'
import { observeServiceInvariant, serviceShapeViolation, type InvariantInstaller } from '@deepseek-ai/dsh-invariants'
import type { Session, SessionEvent } from '@deepseek-ai/dsh-session'
import type { InvariantInstaller } from '@deepseek-ai/dsh-invariants'
import { SANDBOX_MODES } from './session-mode.ts'
const PACKAGE_NAME = '@deepseek-ai/dsh-bash'
/** Cordis companion plugin name. */
export const name = 'bash-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. */
/** Install validation for the durable sandbox-mode vocabulary. */
const install: InvariantInstaller = (ctx, fail) => {
observeServiceInvariant(ctx, fail, 'bash', value => serviceShapeViolation(value, {
methods: ['resolve', 'run', 'start'],
}))
ctx.on('internal/dispatch', (_mode, eventName, args) => {
if (eventName !== 'session/event') return
const event = (args as [Session, SessionEvent])[1]
if (event.type === 'bash/sandbox-mode' && !SANDBOX_MODES.includes(event.data.mode)) {
fail(`bash/sandbox-mode carries unknown mode ${JSON.stringify(event.data.mode)}`)
}
}, { global: true })
}
/**
* Register this package's invariant companion.
* Register the bash invariant companion.
* @param ctx - Cordis context carrying the invariant service.
* @returns the installed registration's disposer after setup succeeds.
*/
@@ -0,0 +1,33 @@
import { describe, expect, it } from 'vitest'
import { Context } from 'cordis'
import type { Session, SessionEvent } from '@deepseek-ai/dsh-session'
import InvariantService from '@deepseek-ai/dsh-invariants'
import * as BashInvariant from '@deepseek-ai/dsh-bash/invariant'
async function setup(): Promise<Context> {
const ctx = new Context()
await ctx.plugin(InvariantService)
await ctx.plugin(BashInvariant)
return ctx
}
function modeEvent(mode: string): SessionEvent {
return { type: 'bash/sandbox-mode', seq: 0, time: 0, data: { mode } } as SessionEvent
}
describe('bash invariants', () => {
it('accepts the closed sandbox vocabulary and ignores unrelated events', async () => {
const ctx = await setup()
expect(() => { ctx.emit('session/event', {} as Session, modeEvent('workspace-write')) }).not.toThrow()
expect(() => { ctx.emit('session/event', {} as Session, {
type: 'turn/start', seq: 0, time: 0, data: {},
} as SessionEvent) }).not.toThrow()
expect(() => { ctx.emit('tools/change') }).not.toThrow()
})
it('rejects an unknown durable sandbox mode', async () => {
const ctx = await setup()
expect(() => { ctx.emit('session/event', {} as Session, modeEvent('host-root')) })
.toThrow(/unknown mode "host-root"/)
})
})