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
+2 -3
View File
@@ -6,6 +6,7 @@
import { parseArgs as parseNodeArgs } from 'node:util'
import { Command } from 'commander'
import { splitForwardedArgs } from './forwarding.ts'
/** Commands implemented by the dsh-sdk launcher. */
type DshSdkCommand = 'start' | 'dev' | 'build' | 'config'
@@ -33,9 +34,7 @@ export function parseDshSdkArgs(argv: readonly string[]): DshSdkArgs {
if (argv.length === 0 || argv[0] === '--help' || argv[0] === '-h') {
return { forwarded: [], help: true }
}
const separator = argv.indexOf('--')
const launcherArgv = separator === -1 ? argv : argv.slice(0, separator)
const passthrough = separator === -1 ? [] : argv.slice(separator + 1)
const { launcher: launcherArgv, forwarded: passthrough } = splitForwardedArgs(argv)
let parsed: DshSdkArgs | undefined
const program = new Command()
.name('dsh-sdk')
+21
View File
@@ -0,0 +1,21 @@
/** Argument-delimiter handling shared by the SDK launcher and its invariant. */
/** Launcher-owned arguments and opaque arguments following `--`. */
export interface ForwardedArgumentSplit {
/** Arguments parsed by the SDK launcher. */
readonly launcher: readonly string[]
/** Arguments passed unchanged to the selected project command. */
readonly forwarded: readonly string[]
}
/**
* Split the first `--` delimiter without interpreting either side.
* @param argv - complete user argument vector.
* @returns launcher arguments and post-delimiter arguments.
*/
export function splitForwardedArgs(argv: readonly string[]): ForwardedArgumentSplit {
const separator = argv.indexOf('--')
return separator === -1
? { launcher: argv, forwarded: [] }
: { launcher: argv.slice(0, separator), forwarded: argv.slice(separator + 1) }
}
+20 -10
View File
@@ -1,14 +1,8 @@
/**
* Generated invariant ownership companion for `@deepseek-ai/dsh-scripts`.
* Replace this file with package-owned checks while preserving its registration.
*
* @generated scripts/gen-package-invariants.ts
* @module @deepseek-ai/dsh-scripts/invariant
*/
/** Package-owned runtime contracts for @deepseek-ai/dsh-scripts. @module @deepseek-ai/dsh-scripts/invariant */
/* jscpd:ignore-start */
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-scripts'
@@ -17,8 +11,24 @@ export const name = 'scripts-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 the launcher's opaque post-separator forwarding boundary. */
const install: InvariantInstaller = (ctx, fail) => {
ctx.effect(async () => {
const { splitForwardedArgs } = await import('./forwarding.ts')
const plain = splitForwardedArgs(['dev', 'src/index.ts'])
const separated = splitForwardedArgs(['dev', 'src/index.ts', '--', '--inspect', '9229'])
assertInvariant(fail,
plain.launcher.length === 2
&& plain.forwarded.length === 0
&& separated.launcher.length === 2
&& separated.launcher[1] === 'src/index.ts'
&& separated.forwarded.length === 2
&& separated.forwarded[0] === '--inspect'
&& separated.forwarded[1] === '9229',
'dsh-sdk must split the first delimiter without interpreting forwarded runtime arguments')
return () => {}
}, 'dsh-sdk: validate command argument contracts')
}
/**
* Register this package's invariant companion.