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,7 @@
/**
* Generated invariant ownership companion for `@deepseek-ai/dsh-compact-basic`.
* Replace this file with package-owned checks while preserving its registration.
*
* @generated scripts/gen-package-invariants.ts
* @module @deepseek-ai/dsh-compact-basic/invariant
*/
/** Package-owned runtime contract checks for `@deepseek-ai/dsh-compact-basic`. @module @deepseek-ai/dsh-compact-basic/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-compact-basic'
@@ -17,8 +10,37 @@ export const name = 'compact-basic-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: 'BasicCompactService',
inject: [
'llm',
'tokenMeter',
],
effects: [
'ctx.provide("compact")',
],
services: [
'compact',
],
validate: (fiber, effectLabels) => {
const automaticEffects = [
'ctx.on("agent/post-step")',
'ctx.on("agent/request-error")',
]
const installed = automaticEffects.filter(label => effectLabels.has(label)).length
const automatic = (fiber.config as { auto?: boolean }).auto !== false
if (automatic && installed !== automaticEffects.length) {
return 'automatic compaction must install both pressure and overflow listeners'
}
if (!automatic && installed !== 0) {
return 'auto:false must install neither automatic compaction listener'
}
return undefined
},
})
}
/**
* Register this package's invariant companion.
@@ -27,4 +49,3 @@ const install: InvariantInstaller = () => {}
*/
export const apply = (ctx: Context): Promise<() => void> =>
Promise.resolve(ctx.invariants.register(PACKAGE_NAME, install))
/* jscpd:ignore-end */

View File

@@ -5,7 +5,7 @@ import type { BasicCompactConfig } from '@deepseek-ai/dsh-compact-basic'
import { selectCompactableRange } from '@deepseek-ai/dsh-compact-basic/src/region.ts'
import { toolPairingBalancedAfter, toolPairingBalancedBefore } from '@deepseek-ai/dsh-compact'
import { resolveConfig } from '@deepseek-ai/dsh-compact-basic/src/config.ts'
import type { CompactionResult } from '@deepseek-ai/dsh-compact'
import type { CompactService, CompactionResult } from '@deepseek-ai/dsh-compact'
import LlmService, { CallId, CONTEXT_WINDOW_EXCEEDED_CODE, LlmAdapter } from '@deepseek-ai/dsh-llm'
import type { ContentBlock, GenerateOptions, StreamChunk } from '@deepseek-ai/dsh-llm'
import { Session, SessionId } from '@deepseek-ai/dsh-session'
@@ -198,6 +198,29 @@ describe('compact configuration and defaults', () => {
expect(() => resolveConfig(config as BasicCompactConfig, ctx.tokenMeter)).toThrow(pattern)
}
})
it.each([
[{ auto: true }, false, /must install both pressure and overflow listeners/],
[{ auto: false }, true, /must install neither automatic compaction listener/],
])('rejects an inconsistent automatic-listener topology through the package invariant', async (config, installListener, message) => {
const ctx = new Context()
await ctx.plugin(LlmService)
await ctx.plugin(TokenMeterService)
const invalidCompact = {
name: 'BasicCompactService',
inject: ['llm', 'tokenMeter'],
apply(child: Context, _config: { auto?: boolean }) {
child.provide('compact', {
compactIfNeeded() {},
compactRegion() {},
} as unknown as CompactService)
if (installListener) {
child.effect(() => () => {}, 'ctx.on("agent/post-step")')
}
},
}
await expect(ctx.plugin(invalidCompact, config)).rejects.toThrow(message)
})
})
describe('pressure measurement and retention', () => {