Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

offset-composition.test-d.ts

119 lines5.0 KB

1/**
2 * Compile-time proof that the REAL durable-stream backend composes with
3 * `@tanstack/ai-sandbox`'s run driver and reaper.
4 *
5 * This is the integration counterpart to
6 * `packages/ai-sandbox/tests/offset-generics.test-d.ts`, which asserts the same
7 * threading against a locally-declared branded offset (the sandbox package must
8 * not depend on a durability backend). Here the actual `durableStream` is used,
9 * so the assertion covers the composition an application really writes.
10 *
11 * THE BUG THIS PINS. `durableStream` returns
12 * `StreamDurability<DurableStreamOffset>`. `StreamDurability` is contravariant
13 * in its offset (`read` takes one), so that type is NOT assignable to
14 * `StreamDurability<string>`. While `SandboxRunDriverOptions.durability` and
15 * `ReapOptions.durability` hardcoded the `string` default, this file's
16 * assignments failed with:
17 *
18 * ```
19 * error TS2322: Type '(runId: string) => StreamDurability<DurableStreamOffset>'
20 * is not assignable to type '(runId: string) => StreamDurability<string>'.
21 * Types of property 'read' are incompatible.
22 * Types of parameters 'offset' and 'offset' are incompatible.
23 * Type 'string' is not assignable to type 'DurableStreamOffset'.
24 * ```
25 *
26 * i.e. the Cloudflare-backed production durability path — the one the sandbox
27 * docs point people at for multi-host durability — could not be wired without a
28 * cast.
29 */
30import { expectTypeOf } from 'vitest'
31import { withSandbox } from '@tanstack/ai-sandbox'
32import type {
33 ReapOptions,
34 SandboxDefinition,
35 SandboxDurabilityOptions,
36 SandboxRunDriverOptions,
37 SandboxRunDurability,
38} from '@tanstack/ai-sandbox'
39import type { RunStore, StreamDurability } from '@tanstack/ai'
40import type { DurableStreamOffset, durableStream } from '../src'
41​
42declare const logFor: (runId: string) => ReturnType<typeof durableStream>
43​
44// The composition itself: a `durableStream` factory is accepted by both option
45// types, with no cast.
46const driverDurability: SandboxRunDriverOptions<DurableStreamOffset>['durability'] =
47 logFor
48const reapDurability: ReapOptions<DurableStreamOffset>['durability'] = logFor
49void driverDurability
50void reapDurability
51​
52// And the offset type FLOWS THROUGH rather than being widened away — an
53// application never writes `TOffset` explicitly, so inference is the part that
54// has to work.
55declare function inferDriver<TOffset extends string>(
56 input: Pick<SandboxRunDriverOptions<TOffset>, 'durability'>,
57): TOffset
58expectTypeOf(
59 inferDriver({ durability: logFor }),
60).toEqualTypeOf<DurableStreamOffset>()
61​
62declare function inferReap<TOffset extends string>(
63 input: Pick<ReapOptions<TOffset>, 'durability'>,
64): TOffset
65expectTypeOf(
66 inferReap({ durability: logFor }),
67).toEqualTypeOf<DurableStreamOffset>()
68​
69// The threaded parameter must NOT have degraded into `string` on the way. If it
70// had, an unvalidated offset would type-check here and `resumeFrom`/`read`
71// offsets would have lost the meaning the brand gives them. Asserted THROUGH
72// the driver's option type, not on the adapter directly, so this also fails if
73// `DurableStreamOffset` itself is ever widened to `string` — the brand is only
74// worth threading while it still excludes something.
75declare const durability: ReturnType<typeof durableStream>
76const asFactoryOffset: StreamDurability<DurableStreamOffset> = durability
77void asFactoryOffset
78​
79declare const threaded: ReturnType<
80 SandboxRunDriverOptions<DurableStreamOffset>['durability']
82// @ts-expect-error an unvalidated string is not a durable-stream cursor
83threaded.read('not-a-cursor')
84expectTypeOf<
85 Parameters<SandboxRunDriverOptions<DurableStreamOffset>['durability']>
86>().toEqualTypeOf<[runId: string]>()
87​
88// ---------------------------------------------------------------------------
89// The other half of the durable path: `withSandbox`'s durability option, i.e.
90// the POST handler that STARTS the run.
91//
92// `SandboxDurabilityOptions.adapter` hardcoded `StreamDurability`, so the exact
93// wiring `docs/sandbox/takeover.md` prescribes could be written on the resume
94// route (via `sandboxRunDriver`, above) but NOT on the route that starts the
95// run — the same TS2322 on `read`, from the opposite end of the same feature.
96// This is what forced the takeover doc's POST-handler snippet to use
97// `memoryStream`.
98// ---------------------------------------------------------------------------
99​
100declare const definition: SandboxDefinition
101declare const runs: RunStore
102​
103const startOption: SandboxDurabilityOptions<DurableStreamOffset> = {
104 adapter: durability,
106void startOption
107​
108// Inference, with no `TOffset` written anywhere — the shape an application
109// actually types.
110withSandbox(definition, { runs, durability: { adapter: durability } })
111withSandbox(definition, {
112 runs,
113 durability: { adapter: durability, attach: true, journal: '/tmp/runs' },
114})
115​
116// A real `durableStream` also satisfies the bus payload's offset-covariant view
117// of a log, which is what lets one concrete capability instantiation carry it.
118const busLog: SandboxRunDurability['adapter'] = durability
119void busLog