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,38 +1,24 @@
/** Package-owned runtime contract checks for `@deepseek-ai/dsh-hooks-codex`. @module @deepseek-ai/dsh-hooks-codex/invariant */
/**
* Package-owned invariant companion for `@deepseek-ai/dsh-hooks-codex`.
* @module @deepseek-ai/dsh-hooks-codex/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-hooks-codex'
/** Cordis companion plugin name. */
export const name = 'hooks-codex-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: 'hooks-codex',
inject: [
'bash',
],
validate: (_fiber, effectLabels) => {
const hookEffects = [
'hooks-codex: drain detached hook runs',
'ctx.on("agent/session-start")',
'ctx.on("agent/prompt-submit")',
'ctx.on("tools/pre-execute")',
'ctx.on("tools/post-execute")',
'ctx.on("agent/turn-continuation")',
]
const installed = hookEffects.filter(label => effectLabels.has(label)).length
return installed === 0 || installed === hookEffects.length
? undefined
: 'a readable Codex hook config must install its complete listener set atomically'
},
})
}
/**
* No runtime invariant: this bridge publishes hook-protocol session events, whose companion owns
* their cross-event provenance relation.
*/
const install: InvariantInstaller = () => {}
/**
* Register this package's invariant companion.
@@ -41,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

@@ -1,26 +0,0 @@
import { describe, expect, it } from 'vitest'
import { Context } from 'cordis'
import type { BashExecutor } from '@deepseek-ai/dsh-bash'
describe('Codex hook package invariant', () => {
it('rejects a partially installed hook listener set', async () => {
const ctx = new Context()
await ctx.plugin({
name: 'codex-invariant-bash',
apply(child: Context) {
child.provide('bash', {
resolve() {},
async run() {},
start() {},
} as unknown as BashExecutor)
},
})
await expect(ctx.plugin({
name: 'hooks-codex',
inject: ['bash'],
apply(child: Context) {
child.effect(() => () => {}, 'ctx.on("agent/session-start")')
},
})).rejects.toThrow(/must install its complete listener set atomically/)
})
})