fix(invariants): replay histories and pack companions

This commit is contained in:
Tianyi Cui
2026-07-21 17:46:43 +08:00
parent 2434afd2ef
commit 504fbf59f3
18 changed files with 322 additions and 74 deletions

View File

@@ -2,7 +2,7 @@
import type { Context } from 'cordis'
import type { Session, SessionEvent } from '@deepseek-ai/dsh-session'
import type { InvariantInstaller } from '@deepseek-ai/dsh-invariants'
import type { InvariantFailure, InvariantInstaller } from '@deepseek-ai/dsh-invariants'
import { SANDBOX_MODES } from './session-mode.ts'
const PACKAGE_NAME = '@deepseek-ai/dsh-sandbox-policy'
@@ -12,16 +12,26 @@ export const name = 'sandbox-policy-invariant'
/** Service required before the companion can reserve package ownership. */
export const inject = ['invariants']
/** Install validation for the durable sandbox-mode vocabulary. */
const install: InvariantInstaller = (ctx, fail) => {
/* jscpd:ignore-start -- package companions share replay and dispatch plumbing */
/** Validate the package-owned event shape and ignore unrelated events. */
function validateEvent(event: SessionEvent, fail: InvariantFailure): void {
if (event.type === 'sandbox/mode' && !SANDBOX_MODES.includes(event.data.mode)) {
fail(`sandbox/mode carries unknown mode ${JSON.stringify(event.data.mode)}`)
}
}
/** Install validation for loaded and newly appended sandbox modes. */
const install: InvariantInstaller = Object.assign((ctx: Context, fail: InvariantFailure) => {
for (const session of ctx.sessions.list()) {
for (const event of session.events) validateEvent(event, fail)
}
ctx.on('internal/dispatch', (_mode, eventName, args) => {
if (eventName !== 'session/event') return
const event = (args as [Session, SessionEvent])[1]
if (event.type === 'sandbox/mode' && !SANDBOX_MODES.includes(event.data.mode)) {
fail(`sandbox/mode carries unknown mode ${JSON.stringify(event.data.mode)}`)
}
validateEvent(event, fail)
}, { global: true })
}
}, { inject: ['sessions'] })
/* jscpd:ignore-end */
/**
* Register this package's invariant companion.

View File

@@ -1,12 +1,13 @@
import { describe, expect, it } from 'vitest'
import { Context } from 'cordis'
import type { Session, SessionEvent } from '@deepseek-ai/dsh-session'
import SessionStore, { type Session, type SessionEvent } from '@deepseek-ai/dsh-session'
import InvariantService, { InvariantError } from '@deepseek-ai/dsh-invariants'
import * as SandboxPolicyInvariant from '@deepseek-ai/dsh-sandbox-policy/invariant'
async function setup(): Promise<Context> {
const ctx = new Context()
await ctx.plugin(InvariantService)
await ctx.plugin(SessionStore)
await ctx.plugin(InvariantService, { enabled: true })
await ctx.plugin(SandboxPolicyInvariant)
return ctx
}
@@ -37,4 +38,16 @@ describe('sandbox-policy invariants', () => {
expect(() => { ctx.emit('session/event', {} as Session, modeEvent('host-root')) })
.toThrow(new InvariantError('@deepseek-ai/dsh-sandbox-policy', 'sandbox/mode carries unknown mode "host-root"'))
})
it('rejects an unknown mode already present on late registration', async () => {
const ctx = new Context()
await ctx.plugin(SessionStore)
ctx.sessions.create().append('sandbox/mode', { mode: 'host-root' as never })
await ctx.plugin(InvariantService, { enabled: true })
await expect(ctx.plugin(SandboxPolicyInvariant).then(() => undefined)).rejects.toMatchObject({
code: 'INVARIANT',
packageName: '@deepseek-ai/dsh-sandbox-policy',
})
})
})

View File

@@ -0,0 +1,25 @@
import { defineConfig } from 'tsdown'
/** Build the package root and invariant companion as independent bundles. */
export default defineConfig([
{
entry: ['lib/types/index.js'],
outDir: 'lib',
format: ['esm'],
platform: 'node',
target: 'es2024',
fixedExtension: false,
dts: false,
clean: false,
},
{
entry: ['lib/types/invariant.js'],
outDir: 'lib',
format: ['esm'],
platform: 'node',
target: 'es2024',
fixedExtension: false,
dts: false,
clean: false,
},
])