import { expect, test } from 'vitest' import { ecpDefaultGrant, ecpPublishBuildFeature, } from '@kody-internal/shared/execution-context-protocol/index.ts' import { backgroundExecutorTimeoutMs, defaultExecutorTimeoutMs, } from '#mcp/executor-budgets.ts' import { runChainShapeError } from './chain-rules.ts' import { contextRunTimeoutMs, describeAttempts, grantTimeoutIsExplicit, describeCloudFallback, describeRanOn, historyRanOn, ranOnFromMetadata, grantRefusal, markQueuedAttemptExpired, markQueuedAttemptRan, lostRunCanMoveOn, planTargets, publishBuildSupported, withRunTiming, sharedRunHostRefusal, typeScriptSourceRefusal, packageRuntimeRefusal, } from './routing.ts' import type { ExecutionContextRow } from './store.ts' const a = { kind: 'context', id: 'ec_a' } as const const b = { kind: 'context', id: 'ec_b' } as const const cloud = { kind: 'cloud' } as const const named = { id: 'ec_b', name: 'build-box' } as ExecutionContextRow test('an unnamed run walks the chain in order and ends on cloud', () => { expect( planTargets({ named: null, fallback: undefined, chain: [a, b, cloud], strict: false, requires: [], }), ).toEqual([a, b, cloud]) }) test('a strict chain never falls back to cloud', () => { expect( planTargets({ named: null, fallback: undefined, chain: [a], strict: true, requires: [], }), ).toEqual([a]) }) test('requires removes cloud', () => { expect( planTargets({ named: null, fallback: undefined, chain: [a, cloud], strict: false, requires: ['fs'], }), ).toEqual([a]) expect( planTargets({ named: 'cloud', fallback: undefined, chain: [], strict: false, requires: ['lan'], }), ).toEqual([]) }) test('a named context never falls back unless fallback is chain', () => { expect( planTargets({ named, fallback: undefined, chain: [a, cloud], strict: false, requires: [], }), ).toEqual([b]) expect( planTargets({ named, fallback: 'chain', chain: [a, b, cloud], strict: false, requires: [], }), ).toEqual([b, a, cloud]) }) test('context: cloud runs on cloud only', () => { expect( planTargets({ named: 'cloud', fallback: undefined, chain: [a, cloud], strict: false, requires: [], }), ).toEqual([cloud]) }) test('requires fs needs both a server grant folder and an advertised local mount', () => { const grant = { ...ecpDefaultGrant, fs: [{ path: '/Users/k/Desktop', mode: 'rw' as const }], } expect( grantRefusal({ grant, requires: ['fs'], sharedRun: false, capabilities: {}, }), ).toBe('no_local_fs') expect( grantRefusal({ grant, requires: ['fs'], sharedRun: false, capabilities: { features: ['fs'] }, }), ).toBeNull() expect( grantRefusal({ grant: ecpDefaultGrant, requires: ['fs'], sharedRun: false, capabilities: { features: ['fs'] }, }), ).toBe('no_fs') }) test('a grant refuses runs it cannot serve', () => { expect( grantRefusal({ sharedRun: false, capabilities: {}, grant: ecpDefaultGrant, requires: ['fs'], }), ).toBe('no_fs') expect( grantRefusal({ sharedRun: false, capabilities: {}, grant: ecpDefaultGrant, requires: ['lan'], }), ).toBe('no_lan') expect( grantRefusal({ grant: ecpDefaultGrant, requires: [], sharedRun: false, capabilities: {}, }), ).toBeNull() }) test('a run chain follows the default chain shape rules', () => { expect(runChainShapeError(['laptop', 'cloud'])).toBeNull() expect(runChainShapeError(['laptop'])).toBeNull() expect(runChainShapeError(['cloud', 'laptop'])?.code).toBe('invalid_request') expect(runChainShapeError(['a', 'a'])?.code).toBe('invalid_request') const names = (n: number) => Array.from({ length: n }, (_, i) => `ctx-${i}`) expect(runChainShapeError(names(9))?.code).toBe('chain_too_long') expect(runChainShapeError(names(9))?.message).toBe( 'A chain holds at most 8 contexts plus Kody cloud. Remove one from this chain, then try again.', ) expect(runChainShapeError(names(8))).toBeNull() expect(runChainShapeError([...names(8), 'cloud'])).toBeNull() expect(runChainShapeError([])?.code).toBe('invalid_request') }) test('an expired queued run records its queued attempt as expired', () => { const metadata = { trigger: 'job', executionContext: { id: 'ec_1', name: 'laptop', attempts: [ { target: 'ec_2', name: 'desk', outcome: 'offline' }, { target: 'ec_1', name: 'laptop', outcome: 'queued' }, ], fellBackToCloud: false, }, } expect(markQueuedAttemptExpired(metadata)).toEqual({ trigger: 'job', executionContext: { ...metadata.executionContext, attempts: [ { target: 'ec_2', name: 'desk', outcome: 'offline' }, { target: 'ec_1', name: 'laptop', outcome: 'expired', reason: 'queue_expired', }, ], }, }) expect(markQueuedAttemptExpired({ trigger: 'job' })).toEqual({ trigger: 'job', }) }) test('a queued run the context took records its queued attempt as ran', () => { const metadata = { executionContext: { id: 'ec_1', name: 'laptop', attempts: [{ target: 'ec_1', name: 'laptop', outcome: 'queued' }], fellBackToCloud: false, }, } expect(markQueuedAttemptRan(metadata)).toEqual({ executionContext: { ...metadata.executionContext, attempts: [{ target: 'ec_1', name: 'laptop', outcome: 'ran' }], }, }) }) test('a finished context run keeps its timing on the run record', () => { const timing = { queueMs: 1, setupMs: 20, executeMs: 3, daemonMs: 24, serverMs: 40, transitMs: 16, } const metadata = { executionContext: { id: 'ec_1', name: 'laptop', attempts: [] }, } expect(withRunTiming(metadata, timing)).toEqual({ executionContext: { ...metadata.executionContext, timing }, }) expect(withRunTiming({}, timing)).toEqual({}) }) test('a cloud fallback says which contexts passed on the run and why', () => { expect( describeCloudFallback({ id: 'ec_box', name: 'build-box', fellBackToCloud: true, attempts: [ { target: 'ec_box', name: 'build-box', outcome: 'rejected', reason: 'grant_mismatch', }, { target: 'ec_gone', outcome: 'skipped', reason: 'gone' }, ], }), ).toBe( 'Ran on Kody cloud after build-box declined it (grant mismatch) and a context that no longer exists was skipped.', ) }) test('describeRanOn names where a run happened with friendly names only', () => { expect(describeRanOn(ranOnFromMetadata(null))).toBe('Ran on Kody cloud.') expect( describeRanOn( ranOnFromMetadata({ id: 'ec_box', name: 'build-box', yours: true, fellBackToCloud: false, attempts: [{ target: 'ec_box', name: 'build-box', outcome: 'ran' }], }), ), ).toBe('Ran on build-box (your machine).') expect( describeRanOn( ranOnFromMetadata({ id: 'ec_team', name: 'team-box', yours: false, fellBackToCloud: false, attempts: [ { target: 'ec_box', name: 'build-box', outcome: 'busy' }, { target: 'ec_team', name: 'team-box', outcome: 'ran' }, ], }), { running: true }, ), ).toBe('Running on team-box (an execution context) after build-box was busy.') expect( describeRanOn( ranOnFromMetadata({ id: null, name: null, fellBackToCloud: true, attempts: [ { target: 'ec_box', name: 'build-box', outcome: 'skipped', reason: 'no_lan', }, ], }), ), ).toBe('Ran on Kody cloud after build-box was skipped (no lan).') expect( describeRanOn( ranOnFromMetadata({ id: 'ec_box', name: 'build-box', yours: true, fellBackToCloud: false, attempts: [{ target: 'ec_box', name: 'build-box', outcome: 'queued' }], }), ), ).toBe('Waiting for build-box (your machine) to take this run.') expect(ranOnFromMetadata({ id: 'ec_box', name: 'build-box' })).toEqual({ kind: 'context', id: 'ec_box', name: 'build-box', attempts: [], fellBackToCloud: false, }) }) test('a missing chain ref that is a raw context id never reaches the text', () => { expect( describeRanOn( ranOnFromMetadata({ id: 'cloud', name: 'Kody cloud', fellBackToCloud: true, attempts: [ { target: 'ec_missing1', name: 'ec_missing1', outcome: 'skipped', reason: 'not_found', }, { target: 'unknown-box', name: 'old-box', outcome: 'skipped' }, { target: 'cloud', name: 'Kody cloud', outcome: 'ran' }, ], }), ), ).toBe( 'Ran on Kody cloud after an execution context was skipped (not found) and old-box was skipped.', ) }) test('run history reports no ranOn for a queued run that expired unrun', () => { const expired = { id: 'ec_queue1', name: 'build-box', fellBackToCloud: false, attempts: [{ target: 'ec_queue1', name: 'build-box', outcome: 'expired' }], } expect(historyRanOn(expired)).toBeNull() expect( historyRanOn({ ...expired, attempts: [{ target: 'ec_queue1', name: 'build-box', outcome: 'ran' }], }), ).toMatchObject({ kind: 'context', id: 'ec_queue1', name: 'build-box' }) }) test('a queued run that revocation ended unrun has no ranOn in history', () => { const metadata = markQueuedAttemptExpired( { executionContext: { id: 'ec_queue1', name: 'build-box', fellBackToCloud: false, attempts: [ { target: 'ec_queue1', name: 'build-box', outcome: 'queued' }, ], }, }, 'context_revoked', ) expect(metadata.executionContext).toMatchObject({ attempts: [{ outcome: 'expired', reason: 'context_revoked' }], }) expect(historyRanOn(metadata.executionContext)).toBeNull() }) test('ranOn carries the timing a context reported, and cloud never does', () => { const timing = { queueMs: 1, setupMs: 20, packageGraphMs: 0, packageGraphCached: true, executeMs: 3, daemonMs: 24, serverMs: 40, transitMs: 16, } expect( ranOnFromMetadata({ id: 'ec_box', name: 'build-box', fellBackToCloud: false, attempts: [{ target: 'ec_box', name: 'build-box', outcome: 'ran' }], timing, }), ).toMatchObject({ kind: 'context', id: 'ec_box', timing }) expect( ranOnFromMetadata({ id: 'ec_box', name: 'build-box', fellBackToCloud: true, attempts: [{ target: 'ec_box', name: 'build-box', outcome: 'busy' }], timing, }), ).not.toHaveProperty('timing') }) test('every credential kind needs execution_context:execute to reach a context', async () => { const { hasContextScope } = await import('./routing.ts') // CLI logins, API tokens and OAuth grants all carry scopes now; none of // them reach a context without the execution-context scope. expect(hasContextScope({ request: { scopes: ['org:execute'] } })).toBe(false) expect( hasContextScope({ request: { scopes: ['org:execute', 'execution_context:execute'] }, }), ).toBe(true) // Unscoped credentials (a signed-in browser session) keep full access. expect(hasContextScope({ request: { scopes: null } })).toBe(true) }) test('host folders stay with the operator: shared runs skip fs and outdated daemons with folders', () => { const withFs = { ...ecpDefaultGrant, fs: [{ path: '/Users/k/Desktop', mode: 'rw' as const }], } const current = { features: ['host-operator-only', 'fs'] } expect( sharedRunHostRefusal({ grant: withFs, requires: ['fs'], capabilities: current, sharedRun: false, }), ).toBeNull() expect( sharedRunHostRefusal({ grant: withFs, requires: ['fs'], capabilities: current, sharedRun: true, }), ).toBe('fs_operator_only') expect( sharedRunHostRefusal({ grant: withFs, requires: [], capabilities: { features: ['fs'] }, sharedRun: true, }), ).toBe('fs_daemon_outdated') // The queue drain on reconnect checks the same rule through grantRefusal. expect( grantRefusal({ grant: withFs, requires: [], capabilities: { features: ['fs'] }, sharedRun: true, }), ).toBe('fs_daemon_outdated') expect( sharedRunHostRefusal({ grant: withFs, requires: [], capabilities: current, sharedRun: true, }), ).toBeNull() expect( sharedRunHostRefusal({ grant: ecpDefaultGrant, requires: [], capabilities: {}, sharedRun: true, }), ).toBeNull() }) test('fs and lan skips name the server grant layer and the command that turns it on', () => { const text = describeAttempts([ { target: 'ec_box', name: 'wsl-box', outcome: 'skipped', reason: 'no_fs', }, { target: 'ec_lan', name: 'lan-box', outcome: 'skipped', reason: 'no_lan', }, ]) expect(text).toContain('grant.fs is off on the server') expect(text).toContain('kody context grant allow --context wsl-box') expect(text).toContain('grant.egress is not public-and-lan') expect(text).toContain('kody context grant lan on --context lan-box') }) test('a paused context is described as paused, not offline', () => { expect( describeAttempts([ { target: 'ec_a', name: 'laptop', outcome: 'paused' }, { target: 'ec_b', name: 'desk', outcome: 'offline' }, ]), ).toContain('laptop: paused') }) test('TypeScript skips a daemon too old to run it, with a please update reason', async () => { const typed = 'export default (params: { n: number }) => params.n * 2' const plain = 'export default async (params) => { await 0; return params?.n }' await expect(typeScriptSourceRefusal(typed, {})).resolves.toBe( 'typescript_daemon_outdated', ) await expect(typeScriptSourceRefusal(plain, {})).resolves.toBeNull() await expect( typeScriptSourceRefusal(typed, { features: ['typescript-source'], }), ).resolves.toBeNull() const text = describeAttempts([ { target: 'ec_old', name: 'old-box', outcome: 'skipped', reason: 'typescript_daemon_outdated', }, ]) expect(text).toContain('old-box') expect(text).toContain('kody daemon update') }) test('saved package code skips a daemon without package runtime services, with a please update reason', () => { const passthrough = `import action from "kody:@acme/digest/run" export default async function main(params) { return await action(params) }` const computed = 'export default async () => (await import(`kody:@acme/digest/run`)).default()' const plain = "import { kody } from 'kody:runtime'\nexport default () => kody.ping({})" expect(packageRuntimeRefusal(passthrough, {})).toBe( 'package_runtime_daemon_outdated', ) expect(packageRuntimeRefusal(computed, {})).toBe( 'package_runtime_daemon_outdated', ) expect(packageRuntimeRefusal(plain, {})).toBeNull() expect( packageRuntimeRefusal(passthrough, { features: ['package-runtime-store'], }), ).toBeNull() const text = describeAttempts([ { target: 'ec_old', name: 'old-box', outcome: 'skipped', reason: 'package_runtime_daemon_outdated', }, ]) expect(text).toContain('old-box') expect(text).toContain('kody daemon update') }) test('publish builds require the server-plan capability and say how to upgrade', () => { expect(publishBuildSupported({})).toBe(false) expect(publishBuildSupported({ features: ['typescript-source'] })).toBe(false) expect(publishBuildSupported({ features: ['publish-build'] })).toBe(false) expect(publishBuildSupported({ features: [ecpPublishBuildFeature] })).toBe( true, ) const text = describeAttempts([ { target: 'ec_nas', name: 'family-vault', outcome: 'skipped', reason: 'publish_build_unsupported', }, ]) expect(text).toContain('family-vault') expect(text).toContain('git') expect(text).toContain('update the Kody CLI on this context') expect(text).toContain('For a released install, run `kody daemon update`') expect(text).toContain( 'For a source or branch install, update the checkout, rebuild the CLI, and restart the daemon from that build', ) expect(text).toContain('--context cloud') }) test('a run its context never started moves on only when routing left a later target', () => { const chain = planTargets({ named: null, fallback: undefined, chain: [{ kind: 'context', id: 'ec_a' }, { kind: 'cloud' }], strict: false, requires: [], }) expect( lostRunCanMoveOn({ mayHaveStarted: false, laterTargets: chain.slice(1) }), ).toBe(true) // A run that may have started is never run a second time. expect( lostRunCanMoveOn({ mayHaveStarted: true, laterTargets: chain.slice(1) }), ).toBe(false) // A named context without fallback: "chain" has nowhere else to go. const named = planTargets({ named: { id: 'ec_a' } as never, fallback: undefined, chain: [{ kind: 'context', id: 'ec_a' }, { kind: 'cloud' }], strict: false, requires: [], }) expect( lostRunCanMoveOn({ mayHaveStarted: false, laterTargets: named.slice(1) }), ).toBe(false) // requires: fs removes cloud, so the lost run stays lost. const needsFs = planTargets({ named: null, fallback: undefined, chain: [{ kind: 'context', id: 'ec_a' }, { kind: 'cloud' }], strict: false, requires: ['fs'], }) expect( lostRunCanMoveOn({ mayHaveStarted: false, laterTargets: needsFs.slice(1) }), ).toBe(false) }) test('a cloud fallback after a lost run says the context dropped before it started the run', () => { expect( describeCloudFallback({ id: 'ec_box', name: 'build-box', fellBackToCloud: true, attempts: [{ target: 'ec_box', name: 'build-box', outcome: 'lost' }], }), ).toBe('Ran on Kody cloud after build-box dropped before it started the run.') }) test('a context run gets its surface cloud budget unless the operator set the grant timeout', () => { const interactive = defaultExecutorTimeoutMs const background = backgroundExecutorTimeoutMs const defaultGrant = ecpDefaultGrant const legacyDefaultRow = { ...ecpDefaultGrant, limits: { ...ecpDefaultGrant.limits, timeoutMs: 90_000 }, } const explicitShort = { ...ecpDefaultGrant, limits: { ...ecpDefaultGrant.limits, timeoutMs: 30_000 }, } const explicitDefault = { ...ecpDefaultGrant, limits: { ...ecpDefaultGrant.limits, timeoutMs: 90_000, timeoutMsExplicit: true, }, } // The same for the operator's own context and one shared from another org: // the limit has no shared-run input. for (const grant of [defaultGrant, legacyDefaultRow]) { expect(grantTimeoutIsExplicit(grant)).toBe(false) expect(contextRunTimeoutMs({ grant, surfaceTimeoutMs: interactive })).toBe( 90_000, ) // jobs, async webhooks, subscriptions, and workflow steps expect(contextRunTimeoutMs({ grant, surfaceTimeoutMs: background })).toBe( 300_000, ) } expect( contextRunTimeoutMs({ grant: explicitShort, surfaceTimeoutMs: background }), ).toBe(30_000) expect( contextRunTimeoutMs({ grant: explicitDefault, surfaceTimeoutMs: background, }), ).toBe(90_000) })