model-runtime-lane-stress.test.mjs
sha256:65ccb454656ea5acdea0a10e559b78bcde1eb6ff753ecc2911bc99d1c3d7cadd
feat(calendar): enforce agent context tiers in retrieval AP…
Human
minor
⚠ breaking
1 day ago
| 1 | /** |
| 2 | * Tier 4 — STRESS: model-runtime-lane high-volume and concurrent invocation |
| 3 | * |
| 4 | * Verifies that the pure functions remain stable, deterministic, and non-mutating |
| 5 | * under high call volume and concurrent (Promise-parallel) invocations. Because |
| 6 | * the functions are synchronous and pure, the stress target is the JS call stack and |
| 7 | * object creation/GC pressure, not network throughput. |
| 8 | * |
| 9 | * Reference: docs/COMPANION-APP-PHASE-1-ADAPTER-SEAM.md §5 (determinism invariant) |
| 10 | */ |
| 11 | import { describe, it } from 'node:test'; |
| 12 | import assert from 'node:assert/strict'; |
| 13 | import { |
| 14 | selectLane, |
| 15 | isManagedLane, |
| 16 | enforceConsentPolicy, |
| 17 | } from '../lib/model-runtime-lane.mjs'; |
| 18 | |
| 19 | const ITERATIONS = 10_000; |
| 20 | |
| 21 | describe('Stress — selectLane', () => { |
| 22 | it(`runs ${ITERATIONS} times with alternating capability sets without throwing`, () => { |
| 23 | const capsSets = [ |
| 24 | { inBrowserAvailable: true }, |
| 25 | { companionAvailable: true }, |
| 26 | { managedKeyAvailable: true }, |
| 27 | { openrouterKeyAvailable: true }, |
| 28 | {}, |
| 29 | { selfHostedAvailable: true }, |
| 30 | ]; |
| 31 | const prefsSets = [ |
| 32 | {}, |
| 33 | { orgPrivacyMode: true }, |
| 34 | { keepOnDevice: true }, |
| 35 | { isDelegate: true, delegatedManagedAllowed: false }, |
| 36 | ]; |
| 37 | for (let i = 0; i < ITERATIONS; i++) { |
| 38 | const caps = capsSets[i % capsSets.length]; |
| 39 | const prefs = prefsSets[i % prefsSets.length]; |
| 40 | const lane = selectLane(caps, prefs); |
| 41 | assert.ok( |
| 42 | ['local', 'self_hosted', 'enterprise', 'openrouter', 'direct_provider', 'disabled'].includes(lane), |
| 43 | `unexpected lane: ${lane}`, |
| 44 | ); |
| 45 | } |
| 46 | }); |
| 47 | |
| 48 | it('does not mutate the input capabilities object across many calls', () => { |
| 49 | const caps = { inBrowserAvailable: true, managedKeyAvailable: true }; |
| 50 | const frozen = JSON.stringify(caps); |
| 51 | for (let i = 0; i < ITERATIONS; i++) selectLane(caps, {}); |
| 52 | assert.equal(JSON.stringify(caps), frozen, 'capabilities object was mutated'); |
| 53 | }); |
| 54 | |
| 55 | it('does not mutate the input preferences object across many calls', () => { |
| 56 | const prefs = { keepOnDevice: true }; |
| 57 | const frozen = JSON.stringify(prefs); |
| 58 | for (let i = 0; i < ITERATIONS; i++) selectLane({}, prefs); |
| 59 | assert.equal(JSON.stringify(prefs), frozen, 'preferences object was mutated'); |
| 60 | }); |
| 61 | }); |
| 62 | |
| 63 | describe('Stress — isManagedLane', () => { |
| 64 | const lanes = ['local', 'self_hosted', 'enterprise', 'openrouter', 'direct_provider', 'disabled']; |
| 65 | |
| 66 | it(`returns consistent results over ${ITERATIONS} calls per lane`, () => { |
| 67 | for (const lane of lanes) { |
| 68 | const expected = lane === 'direct_provider'; |
| 69 | for (let i = 0; i < ITERATIONS; i++) { |
| 70 | assert.equal(isManagedLane(lane), expected); |
| 71 | } |
| 72 | } |
| 73 | }); |
| 74 | }); |
| 75 | |
| 76 | describe('Stress — enforceConsentPolicy', () => { |
| 77 | it(`handles ${ITERATIONS} interleaved policy evaluations without state leakage`, () => { |
| 78 | const cases = [ |
| 79 | { lane: 'direct_provider', containsPrivateData: false, consentId: undefined, isDelegate: false, delegatedManagedAllowed: false, expected: 'allow' }, |
| 80 | { lane: 'direct_provider', containsPrivateData: true, consentId: undefined, isDelegate: false, delegatedManagedAllowed: false, expected: 'cloud_consent_required' }, |
| 81 | { lane: 'direct_provider', containsPrivateData: true, consentId: 'cid', isDelegate: false, delegatedManagedAllowed: false, expected: 'allow' }, |
| 82 | { lane: 'direct_provider', containsPrivateData: false, consentId: undefined, isDelegate: true, delegatedManagedAllowed: false, expected: 'lane_policy_denied' }, |
| 83 | { lane: 'local', containsPrivateData: true, consentId: undefined, isDelegate: true, delegatedManagedAllowed: false, expected: 'allow' }, |
| 84 | ]; |
| 85 | for (let i = 0; i < ITERATIONS; i++) { |
| 86 | const c = cases[i % cases.length]; |
| 87 | assert.equal(enforceConsentPolicy(c), c.expected); |
| 88 | } |
| 89 | }); |
| 90 | }); |
| 91 | |
| 92 | describe('Stress — concurrent Promise.all invocation', () => { |
| 93 | it('100 concurrent pipeline invocations complete correctly', async () => { |
| 94 | const tasks = Array.from({ length: 100 }, (_, i) => { |
| 95 | const hasLocal = i % 3 === 0; |
| 96 | const isManaged = !hasLocal; |
| 97 | return Promise.resolve().then(() => { |
| 98 | const caps = hasLocal |
| 99 | ? { inBrowserAvailable: true } |
| 100 | : { managedKeyAvailable: true }; |
| 101 | const lane = selectLane(caps, {}); |
| 102 | const decision = enforceConsentPolicy({ |
| 103 | lane, |
| 104 | containsPrivateData: true, |
| 105 | consentId: undefined, |
| 106 | isDelegate: false, |
| 107 | delegatedManagedAllowed: false, |
| 108 | }); |
| 109 | return { lane, decision, isManaged }; |
| 110 | }); |
| 111 | }); |
| 112 | const results = await Promise.all(tasks); |
| 113 | for (const r of results) { |
| 114 | assert.ok( |
| 115 | ['local', 'direct_provider'].includes(r.lane), |
| 116 | `unexpected lane: ${r.lane}`, |
| 117 | ); |
| 118 | if (r.lane === 'local') assert.equal(r.decision, 'allow'); |
| 119 | if (r.lane === 'direct_provider') assert.equal(r.decision, 'cloud_consent_required'); |
| 120 | } |
| 121 | }); |
| 122 | }); |
File History
2 commits
sha256:65ccb454656ea5acdea0a10e559b78bcde1eb6ff753ecc2911bc99d1c3d7cadd
feat(calendar): enforce agent context tiers in retrieval AP…
Human
minor
⚠
1 day ago
sha256:9103f98c89257ed2b01c237cea895dabb3e85ea337dccb1161c175e4422355b6
docs: accept Calendar Events v0 spec with Phase 0 security …
Human
1 day ago