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

View File

@@ -1,46 +1,24 @@
/** Package-owned runtime contract checks for `@deepseek-ai/dsh-compact-basic`. @module @deepseek-ai/dsh-compact-basic/invariant */
/**
* Package-owned invariant companion for `@deepseek-ai/dsh-compact-basic`.
* @module @deepseek-ai/dsh-compact-basic/invariant
*/
/* jscpd:ignore-start */
import type { Context } from 'cordis'
import { observePluginInvariant, type InvariantInstaller } from '@deepseek-ai/dsh-invariants'
import type { InvariantInstaller } from '@deepseek-ai/dsh-invariants'
const PACKAGE_NAME = '@deepseek-ai/dsh-compact-basic'
/** Cordis companion plugin name. */
export const name = 'compact-basic-invariant'
/** Services required before the companion can register. */
/** Service required before the companion can reserve package ownership. */
export const inject = ['invariants']
/** 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
},
})
}
/**
* 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.
@@ -49,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 */

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 { CompactService, CompactionResult } from '@deepseek-ai/dsh-compact'
import type { 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'
@@ -199,28 +199,6 @@ describe('compact configuration and defaults', () => {
}
})
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', () => {