|
| 1 | +import type { DeviceInfo } from '@agent-device/kernel/device'; |
| 2 | +import type { DeviceLease } from '@agent-device/contracts/device'; |
| 3 | +import { expectTypeOf, test } from 'vitest'; |
| 4 | +import { buildActionEventResult } from '../session-event-action-presentation.ts'; |
| 5 | +import { |
| 6 | + buildRequestFinishedEvent, |
| 7 | + buildRequestStartedEvent, |
| 8 | + shouldRecordEventForRequest, |
| 9 | +} from '../session-event-log.ts'; |
| 10 | +import { |
| 11 | + buildRequestSuccessEventPresentation, |
| 12 | + readRequestedScreenshotFileName, |
| 13 | +} from '../session-event-request.ts'; |
| 14 | + |
| 15 | +/** |
| 16 | + * Every request-shaped parameter this package accepts, read off the real signatures rather than |
| 17 | + * restated. Widening any one of them back to the daemon's own `DaemonRequest` — the type that |
| 18 | + * carries `internal` with its `SessionState` callbacks and admitted `DeviceLease` — changes this |
| 19 | + * union, and the assertions below stop holding. |
| 20 | + */ |
| 21 | +type JournalRequestInput = |
| 22 | + | Parameters<typeof buildRequestStartedEvent>[0]['req'] |
| 23 | + | Parameters<typeof buildRequestFinishedEvent>[0]['req'] |
| 24 | + | Parameters<typeof shouldRecordEventForRequest>[0] |
| 25 | + | Parameters<typeof buildRequestSuccessEventPresentation>[0] |
| 26 | + | Parameters<typeof readRequestedScreenshotFileName>[0] |
| 27 | + | Parameters<typeof buildActionEventResult>[0]; |
| 28 | + |
| 29 | +type Primitive = string | number | boolean | bigint | symbol | null | undefined; |
| 30 | + |
| 31 | +/** |
| 32 | + * Recursion budget. Six is not asserted to be deep enough by inspection: the last assertion in |
| 33 | + * this file walks again at twelve and requires the two answers to be identical, so a budget that |
| 34 | + * stopped short of some path would show up as a type the deeper walk found and this one did not. |
| 35 | + */ |
| 36 | +type Budget = [0, 0, 0, 0, 0, 0]; |
| 37 | + |
| 38 | +/** Twice the budget, for the saturation check. */ |
| 39 | +type DoubleBudget = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; |
| 40 | + |
| 41 | +/** |
| 42 | + * `T`'s own declared keys, with index signatures dropped. Keeping `string` would silently absorb |
| 43 | + * every literal beside it in a union (`string | 'internal'` reduces to `string`), which would make |
| 44 | + * the assertion below vacuous the moment a `Record<string, unknown>` appears on a path — and one |
| 45 | + * does, at `flags`. A record that could hold an `internal` property is not a declared route to |
| 46 | + * session state; a declared `internal` key is. |
| 47 | + */ |
| 48 | +type DeclaredKeys<T> = keyof { [K in keyof T as string extends K ? never : K]: 0 }; |
| 49 | + |
| 50 | +/** Every declared property NAME reachable from `T`, arrays entered through their element type. */ |
| 51 | +type ReachableKeys< |
| 52 | + T, |
| 53 | + Cap extends readonly unknown[], |
| 54 | + Depth extends readonly unknown[] = [], |
| 55 | +> = Depth['length'] extends Cap['length'] |
| 56 | + ? never |
| 57 | + : [T] extends [Primitive] |
| 58 | + ? never |
| 59 | + : [T] extends [(...args: never[]) => unknown] |
| 60 | + ? never |
| 61 | + : T extends readonly (infer Element)[] |
| 62 | + ? ReachableKeys<Element, Cap, [...Depth, 0]> |
| 63 | + : T extends object |
| 64 | + ? |
| 65 | + | DeclaredKeys<T> |
| 66 | + | { [K in keyof T]-?: ReachableKeys<NonNullable<T[K]>, Cap, [...Depth, 0]> }[keyof T] |
| 67 | + : never; |
| 68 | + |
| 69 | +/** |
| 70 | + * Every property TYPE reachable from `T`. Arrays are entered through their element type, and a |
| 71 | + * function through its PARAMETERS: that is how the daemon's `internal` actually hands out a live |
| 72 | + * session record (`beforeDispatch?: (session: SessionState) => …`), so a walk that treated a |
| 73 | + * callback as a leaf would never see the very route this file exists to reject. |
| 74 | + */ |
| 75 | +type ReachableTypes< |
| 76 | + T, |
| 77 | + Cap extends readonly unknown[], |
| 78 | + Depth extends readonly unknown[] = [], |
| 79 | +> = Depth['length'] extends Cap['length'] |
| 80 | + ? never |
| 81 | + : [T] extends [Primitive] |
| 82 | + ? never |
| 83 | + : [T] extends [(...args: never[]) => unknown] |
| 84 | + ? ReachableTypes<Parameters<T>, Cap, [...Depth, 0]> |
| 85 | + : T extends readonly (infer Element)[] |
| 86 | + ? Element | ReachableTypes<Element, Cap, [...Depth, 0]> |
| 87 | + : T extends object |
| 88 | + ? { |
| 89 | + [K in keyof T]-?: |
| 90 | + | NonNullable<T[K]> |
| 91 | + | ReachableTypes<NonNullable<T[K]>, Cap, [...Depth, 0]>; |
| 92 | + }[keyof T] |
| 93 | + : never; |
| 94 | + |
| 95 | +/** |
| 96 | + * The structural minimum of `src/daemon/session-state.ts`'s `SessionState`. The package may not |
| 97 | + * import that module (R11), so the live session record is named by the three required fields no |
| 98 | + * other shape in the public request vocabulary carries together. Anything typed as `SessionState` |
| 99 | + * is assignable to this, which is what `Extract` needs. |
| 100 | + */ |
| 101 | +type LiveSessionRecord = { name: string; device: DeviceInfo; createdAt: number }; |
| 102 | + |
| 103 | +/** A callback of any arity — the form `SessionState` takes when it reaches a request. */ |
| 104 | +type AnyCallback = (...args: never[]) => unknown; |
| 105 | + |
| 106 | +test('the journal reads a request shape with no route to daemon-private session state', () => { |
| 107 | + // ADR 0018's journal is durable and rendered outside the daemon. Its input is the public |
| 108 | + // request vocabulary of `@agent-device/kernel/contracts`: `command`, `flags`, `meta.requestId` |
| 109 | + // and `meta.clientArtifactPaths`. The daemon's own request type adds `internal`, whose members |
| 110 | + // hand out `SessionState` and an admitted `DeviceLease`; taking it here would put a live |
| 111 | + // session record on a path the journal could serialize. |
| 112 | + expectTypeOf< |
| 113 | + Extract<ReachableKeys<JournalRequestInput, Budget>, 'internal'> |
| 114 | + >().toEqualTypeOf<never>(); |
| 115 | + expectTypeOf< |
| 116 | + Extract<ReachableTypes<JournalRequestInput, Budget>, LiveSessionRecord> |
| 117 | + >().toEqualTypeOf<never>(); |
| 118 | + expectTypeOf< |
| 119 | + Extract<ReachableTypes<JournalRequestInput, Budget>, DeviceLease> |
| 120 | + >().toEqualTypeOf<never>(); |
| 121 | + expectTypeOf< |
| 122 | + Extract<ReachableTypes<JournalRequestInput, Budget>, AnyCallback> |
| 123 | + >().toEqualTypeOf<never>(); |
| 124 | +}); |
| 125 | + |
| 126 | +/** |
| 127 | + * The budget is a fixed point, not a guess. Walking again at twice the depth must reveal no key |
| 128 | + * and no type the six-deep walk missed; if the public request vocabulary ever grows a path deeper |
| 129 | + * than the budget, this fails here rather than quietly narrowing the assertions above. |
| 130 | + * |
| 131 | + * What the walk deliberately does not see: a route through an index signature. `flags` and `input` |
| 132 | + * are `Record<string, unknown>`, so a value stashed under a record key is invisible to a type-level |
| 133 | + * walk by construction — `DeclaredKeys` drops index signatures precisely so their `string` cannot |
| 134 | + * absorb the literal keys beside it. The claim is about DECLARED routes, which is what a type can |
| 135 | + * carry; a record that could hold anything is not one. |
| 136 | + */ |
| 137 | +test('the six-deep walk is saturated: twice the budget finds nothing more', () => { |
| 138 | + expectTypeOf< |
| 139 | + Exclude< |
| 140 | + ReachableKeys<JournalRequestInput, DoubleBudget>, |
| 141 | + ReachableKeys<JournalRequestInput, Budget> |
| 142 | + > |
| 143 | + >().toEqualTypeOf<never>(); |
| 144 | + expectTypeOf< |
| 145 | + Exclude< |
| 146 | + ReachableTypes<JournalRequestInput, DoubleBudget>, |
| 147 | + ReachableTypes<JournalRequestInput, Budget> |
| 148 | + > |
| 149 | + >().toEqualTypeOf<never>(); |
| 150 | +}); |
0 commit comments