fix(invariants): join package checks at startup

This commit is contained in:
Tianyi Cui
2026-07-20 01:53:09 +08:00
parent 684fcf3357
commit e80fc3e61b
31 changed files with 596 additions and 314 deletions

View File

@@ -12,24 +12,23 @@ export const name = 'create-sdk-invariant'
export const inject = ['invariants']
/** Assert the bin-only entrypoint and its core argument mapping. */
const install: InvariantInstaller = (ctx, fail) => {
ctx.effect(async () => {
const { parseCreateArgs } = await import('./args.ts')
const packageEntry = await import('./index.ts')
assertInvariant(fail, Object.keys(packageEntry).length === 0,
'the create-sdk library entrypoint must remain empty because the package is bin-only')
const parsed = parseCreateArgs([
'workspace', '--provider=custom', '--base-url=https://example.test', '--interface=embed', '--no-install',
])
assertInvariant(fail,
parsed.directory === 'workspace'
&& parsed.provider === 'custom'
&& parsed.baseURL === 'https://example.test'
&& parsed.runInterface === 'embed'
&& parsed.install === false,
'create-sdk arguments must preserve directory, provider, base URL, interface, and negative install flags')
return () => {}
}, 'create-sdk: validate bin and argument contracts')
const install: InvariantInstaller = async (_ctx, fail) => {
const [{ parseCreateArgs }, packageEntry] = await Promise.all([
import('./args.ts'),
import('./index.ts'),
])
assertInvariant(fail, Object.keys(packageEntry).length === 0,
'the create-sdk library entrypoint must remain empty because the package is bin-only')
const parsed = parseCreateArgs([
'workspace', '--provider=custom', '--base-url=https://example.test', '--interface=embed', '--no-install',
])
assertInvariant(fail,
parsed.directory === 'workspace'
&& parsed.provider === 'custom'
&& parsed.baseURL === 'https://example.test'
&& parsed.runInterface === 'embed'
&& parsed.install === false,
'create-sdk arguments must preserve directory, provider, base URL, interface, and negative install flags')
}
/**

View File

@@ -12,20 +12,17 @@ export const name = 'helper-invariant'
export const inject = ['invariants']
/** Assert FeatureId's zero-cost representation and boundary validation. */
const install: InvariantInstaller = (ctx, fail) => {
ctx.effect(async () => {
const { featureId } = await import('./ids.ts')
assertInvariant(fail, featureId('local-plugin') === 'local-plugin',
'a valid feature id must preserve its runtime string value')
let rejected = false
try {
featureId('Invalid Feature')
} catch (error) {
rejected = error instanceof Error
}
assertInvariant(fail, rejected, 'feature ids must reject values outside lowercase kebab-case')
return () => {}
}, 'dsh-helper: validate feature identities')
const install: InvariantInstaller = async (_ctx, fail) => {
const { featureId } = await import('./ids.ts')
assertInvariant(fail, featureId('local-plugin') === 'local-plugin',
'a valid feature id must preserve its runtime string value')
let rejected = false
try {
featureId('Invalid Feature')
} catch (error) {
rejected = error instanceof Error
}
assertInvariant(fail, rejected, 'feature ids must reject values outside lowercase kebab-case')
}
/**

View File

@@ -12,22 +12,19 @@ export const name = 'scripts-invariant'
export const inject = ['invariants']
/** 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')
const install: InvariantInstaller = async (_ctx, fail) => {
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')
}
/**

View File

@@ -12,19 +12,16 @@ export const name = 'telemetry-invariant'
export const inject = ['invariants']
/** Assert the final telemetry redaction boundary removes secrets without corrupting ordinary package metadata. */
const install: InvariantInstaller = (ctx, fail) => {
ctx.effect(async () => {
const [{ DEFAULT_REDACTION_PLACEHOLDER, SecretRedactor }, { telemetryRedactionViolation }] = await Promise.all([
import('./secret-redactor.ts'),
import('./redaction-contract.ts'),
])
const redactor = new SecretRedactor()
const violation = telemetryRedactionViolation(redactor, DEFAULT_REDACTION_PLACEHOLDER, PACKAGE_NAME)
assertInvariant(fail,
violation === undefined,
violation ?? 'telemetry redaction contract failed without a diagnostic')
return () => {}
}, 'telemetry: validate secret-redaction boundary')
const install: InvariantInstaller = async (_ctx, fail) => {
const [{ telemetryRedactionViolation }, { DEFAULT_REDACTION_PLACEHOLDER, SecretRedactor }] = await Promise.all([
import('./redaction-contract.ts'),
import('./secret-redactor.ts'),
])
const redactor = new SecretRedactor()
const violation = telemetryRedactionViolation(redactor, DEFAULT_REDACTION_PLACEHOLDER, PACKAGE_NAME)
assertInvariant(fail,
violation === undefined,
violation ?? 'telemetry redaction contract failed without a diagnostic')
}
/**