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,13 +12,10 @@ export const name = 'brand-invariant'
export const inject = ['invariants']
/** Assert that the nominal-type primitive remains erased at runtime. */
const install: InvariantInstaller = (ctx, fail) => {
ctx.effect(async () => {
const brandRuntime = await import('./index.ts')
assertInvariant(fail, Object.keys(brandRuntime).length === 0,
'the branded-id primitive must remain type-only with no runtime exports')
return () => {}
}, 'brand: validate type-only runtime erasure')
const install: InvariantInstaller = async (_ctx, fail) => {
const brandRuntime = await import('./index.ts')
assertInvariant(fail, Object.keys(brandRuntime).length === 0,
'the branded-id primitive must remain type-only with no runtime exports')
}
/**

View File

@@ -13,17 +13,14 @@ export const name = 'home-invariant'
export const inject = ['invariants']
/** Assert the canonical environment key and configured-path precedence. */
const install: InvariantInstaller = (ctx, fail) => {
ctx.effect(async () => {
const { DSH_HOME_ENV, resolveDshHome } = await import('./index.ts')
const environmentKey: string = DSH_HOME_ENV
assertInvariant(fail, environmentKey === ['DSH', 'HOME'].join('_'),
'the canonical Harness home environment key must remain DSH_HOME')
const configured = 'relative-invariant-home'
assertInvariant(fail, resolveDshHome(configured) === resolve(configured),
'an explicitly configured Harness home must normalize to an absolute path')
return () => {}
}, 'home: validate canonical DSH home resolution')
const install: InvariantInstaller = async (_ctx, fail) => {
const { DSH_HOME_ENV, resolveDshHome } = await import('./index.ts')
const environmentKey: string = DSH_HOME_ENV
assertInvariant(fail, environmentKey === ['DSH', 'HOME'].join('_'),
'the canonical Harness home environment key must remain DSH_HOME')
const configured = 'relative-invariant-home'
assertInvariant(fail, resolveDshHome(configured) === resolve(configured),
'an explicitly configured Harness home must normalize to an absolute path')
}
/**

View File

@@ -14,17 +14,14 @@ export const name = 'paths-invariant'
export const inject = ['invariants']
/** Assert tilde expansion and explicit-over-environment home precedence. */
const install: InvariantInstaller = (ctx, fail) => {
ctx.effect(async () => {
const { DSH_HOME_ENV, expandHomePath, resolveDshHome } = await import('./index.ts')
assertInvariant(fail, expandHomePath('~/invariant-probe') === join(homedir(), 'invariant-probe'),
'supported tilde prefixes must expand against the operating-system home')
const configured = 'relative-invariant-home'
const resolved = resolveDshHome(configured, { [DSH_HOME_ENV]: '/ignored-environment-home' })
assertInvariant(fail, resolved === resolve(configured),
'an explicit DSH home must override the environment and normalize to an absolute path')
return () => {}
}, 'paths: validate DSH home resolution')
const install: InvariantInstaller = async (_ctx, fail) => {
const { DSH_HOME_ENV, expandHomePath, resolveDshHome } = await import('./index.ts')
assertInvariant(fail, expandHomePath('~/invariant-probe') === join(homedir(), 'invariant-probe'),
'supported tilde prefixes must expand against the operating-system home')
const configured = 'relative-invariant-home'
const resolved = resolveDshHome(configured, { [DSH_HOME_ENV]: '/ignored-environment-home' })
assertInvariant(fail, resolved === resolve(configured),
'an explicit DSH home must override the environment and normalize to an absolute path')
}
/**

View File

@@ -12,24 +12,21 @@ export const name = 'retention-invariant'
export const inject = ['invariants']
/** Assert exact head-retention accounting after the budget is exceeded. */
const install: InvariantInstaller = (ctx, fail) => {
ctx.effect(async () => {
const { ItemRetainer } = await import('./index.ts')
const retainer = new ItemRetainer<string>({ kind: 'head', maxItems: 2 })
retainer.push('first')
retainer.push('second')
retainer.push('third')
const result = retainer.finish()
assertInvariant(fail,
result.items.join(',') === 'first,second'
&& result.seen === 3
&& result.kept === 2
&& result.truncated
&& result.omitted.kind === 'exact'
&& result.omitted.count === 1,
'head retention must keep the prefix and report exact seen, kept, and omitted counts')
return () => {}
}, 'retention: validate exact head accounting')
const install: InvariantInstaller = async (_ctx, fail) => {
const { ItemRetainer } = await import('./index.ts')
const retainer = new ItemRetainer<string>({ kind: 'head', maxItems: 2 })
retainer.push('first')
retainer.push('second')
retainer.push('third')
const result = retainer.finish()
assertInvariant(fail,
result.items.join(',') === 'first,second'
&& result.seen === 3
&& result.kept === 2
&& result.truncated
&& result.omitted.kind === 'exact'
&& result.omitted.count === 1,
'head retention must keep the prefix and report exact seen, kept, and omitted counts')
}
/**

View File

@@ -12,19 +12,16 @@ export const name = 'timeout-invariant'
export const inject = ['invariants']
/** Assert default-before-cap arithmetic and capability-code classification. */
const install: InvariantInstaller = (ctx, fail) => {
ctx.effect(async () => {
const { clampTimeout, TimeoutReason, timeoutOf } = await import('./index.ts')
assertInvariant(fail,
clampTimeout(undefined, 50, 30) === 30 && clampTimeout(20, 50, 30) === 20,
'timeout resolution must apply the default before capping and preserve smaller requests')
const reason = new TimeoutReason('INVARIANT_TIMEOUT', 25)
assertInvariant(fail, timeoutOf({ reason }, 'INVARIANT_TIMEOUT') === reason,
'timeout classification must recover a matching capability-owned reason')
assertInvariant(fail, timeoutOf({ reason }, 'FOREIGN_TIMEOUT') === undefined,
'timeout classification must reject a reason owned by another capability')
return () => {}
}, 'timeout: validate resolution and reason classification')
const install: InvariantInstaller = async (_ctx, fail) => {
const { clampTimeout, TimeoutReason, timeoutOf } = await import('./index.ts')
assertInvariant(fail,
clampTimeout(undefined, 50, 30) === 30 && clampTimeout(20, 50, 30) === 20,
'timeout resolution must apply the default before capping and preserve smaller requests')
const reason = new TimeoutReason('INVARIANT_TIMEOUT', 25)
assertInvariant(fail, timeoutOf({ reason }, 'INVARIANT_TIMEOUT') === reason,
'timeout classification must recover a matching capability-owned reason')
assertInvariant(fail, timeoutOf({ reason }, 'FOREIGN_TIMEOUT') === undefined,
'timeout classification must reject a reason owned by another capability')
}
/**