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

@@ -0,0 +1,23 @@
/** Snapshot-aware application configuration path selection. @module @deepseek-ai/dsh-app-boot/config-path */
import { basename, dirname, resolve } from 'node:path'
/**
* Resolve the config to boot. Replay swaps a `cordis.yml` basename for
* `cordis.snapshot.yml` in the same directory; every other mode keeps the path.
* @param configPath - requested config path, absolute or relative to `cwd`.
* @param snapshotMode - bin `$DSH_SNAPSHOT`; only `replay` swaps the basename.
* @param cwd - base for a relative `configPath`.
* @returns the absolute path of the config to boot.
*/
export function resolveConfigPath(
configPath: string,
snapshotMode: string | undefined,
cwd: string = process.cwd(),
): string {
const absolute = resolve(cwd, configPath)
if (snapshotMode !== 'replay') return absolute
const dir = dirname(absolute)
const replayName = basename(absolute).replace(/cordis\.ya?ml$/, 'cordis.snapshot.yml')
return resolve(dir, replayName)
}

View File

@@ -6,29 +6,12 @@
*/
import { pathToFileURL } from 'node:url'
import { basename, dirname, resolve } from 'node:path'
import { dirname, resolve } from 'node:path'
import { Context } from 'cordis'
import Loader from '@cordisjs/plugin-loader'
import Include from '@cordisjs/plugin-include'
/**
* Resolve the config to boot. Replay swaps a `cordis.yml` basename for
* `cordis.snapshot.yml` in the same directory; every other mode keeps the path.
* @param configPath - the requested config path (absolute, or relative to `cwd`).
* @param snapshotMode - the bin's `$DSH_SNAPSHOT` value; only `'replay'` swaps the
* basename.
* @param cwd - the base a relative `configPath` resolves against.
* @returns the absolute path of the config to boot.
*/
export function resolveConfigPath(
configPath: string, snapshotMode: string | undefined, cwd: string = process.cwd(),
): string {
const absolute = resolve(cwd, configPath)
if (snapshotMode !== 'replay') return absolute
const dir = dirname(absolute)
const replayName = basename(absolute).replace(/cordis\.ya?ml$/, 'cordis.snapshot.yml')
return resolve(dir, replayName)
}
export { resolveConfigPath } from './config-path.ts'
/**
* Load the optional gitignored `.env` from `dir`. Missing files fall back to the

View File

@@ -1,14 +1,9 @@
/**
* Generated invariant ownership companion for `@deepseek-ai/dsh-app-boot`.
* Replace this file with package-owned checks while preserving its registration.
*
* @generated scripts/gen-package-invariants.ts
* @module @deepseek-ai/dsh-app-boot/invariant
*/
/** Package-owned runtime contracts for @deepseek-ai/dsh-app-boot. @module @deepseek-ai/dsh-app-boot/invariant */
/* jscpd:ignore-start */
import { resolve } from 'node:path'
import type { Context } from 'cordis'
import type { InvariantInstaller } from '@deepseek-ai/dsh-invariants'
import { assertInvariant, type InvariantInstaller } from '@deepseek-ai/dsh-invariants'
const PACKAGE_NAME = '@deepseek-ai/dsh-app-boot'
@@ -17,8 +12,20 @@ export const name = 'app-boot-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 = () => {}
/** Assert ordinary and replay config-path selection. */
const install: InvariantInstaller = (ctx, fail) => {
ctx.effect(async () => {
const { resolveConfigPath } = await import('./config-path.ts')
const cwd = '/tmp/dsh-app-boot-invariant'
const ordinary = resolveConfigPath('cordis.yml', undefined, cwd)
const replay = resolveConfigPath('cordis.yml', 'replay', cwd)
assertInvariant(fail, ordinary === resolve(cwd, 'cordis.yml'),
'ordinary app boot must retain the requested config basename')
assertInvariant(fail, replay === resolve(cwd, 'cordis.snapshot.yml'),
'snapshot replay must select cordis.snapshot.yml in the requested config directory')
return () => {}
}, 'app-boot: validate ordinary and replay config selection')
}
/**
* Register this package's invariant companion.