|
| 1 | +/** |
| 2 | + * Dispatcher — routes tool calls to either native TypeScript handlers or the |
| 3 | + * Python bridge (fallback). This is the Strangler Fig migration layer. |
| 4 | + * |
| 5 | + * Feature flags: |
| 6 | + * ALTIMATE_NATIVE_ONLY=1 — throws if no native handler (CI gate) |
| 7 | + * ALTIMATE_SHADOW_MODE=1 — runs both native + bridge, logs mismatches |
| 8 | + */ |
| 9 | + |
| 10 | +import { Bridge } from "../bridge/client" |
| 11 | +import { BridgeMethods, type BridgeMethod } from "../bridge/protocol" |
| 12 | +import { Log } from "../../util/log" |
| 13 | + |
| 14 | +type NativeHandler = (params: any) => Promise<any> |
| 15 | + |
| 16 | +const nativeHandlers = new Map<string, NativeHandler>() |
| 17 | + |
| 18 | +/** Register a native TypeScript handler for a bridge method. */ |
| 19 | +export function register(method: string, handler: NativeHandler): void { |
| 20 | + nativeHandlers.set(method, handler) |
| 21 | +} |
| 22 | + |
| 23 | +/** Dispatch a method call to native handler or bridge fallback. */ |
| 24 | +export async function call<M extends BridgeMethod>( |
| 25 | + method: M, |
| 26 | + params: (typeof BridgeMethods)[M] extends { params: infer P } ? P : never, |
| 27 | +): Promise<(typeof BridgeMethods)[M] extends { result: infer R } ? R : never> { |
| 28 | + const native = nativeHandlers.get(method as string) |
| 29 | + |
| 30 | + if (native) { |
| 31 | + if (process.env.ALTIMATE_SHADOW_MODE === "1") { |
| 32 | + // Parity testing: run both native and bridge, log mismatches |
| 33 | + try { |
| 34 | + const [nativeResult, bridgeResult] = await Promise.allSettled([ |
| 35 | + native(params), |
| 36 | + Bridge.call(method, params), |
| 37 | + ]) |
| 38 | + if ( |
| 39 | + nativeResult.status === "fulfilled" && |
| 40 | + bridgeResult.status === "fulfilled" |
| 41 | + ) { |
| 42 | + const nativeJson = JSON.stringify(nativeResult.value, null, 0) |
| 43 | + const bridgeJson = JSON.stringify(bridgeResult.value, null, 0) |
| 44 | + if (nativeJson !== bridgeJson) { |
| 45 | + Log.Default.warn("shadow mode mismatch", { |
| 46 | + method: String(method), |
| 47 | + native: nativeJson.slice(0, 500), |
| 48 | + bridge: bridgeJson.slice(0, 500), |
| 49 | + }) |
| 50 | + } |
| 51 | + } |
| 52 | + if (nativeResult.status === "fulfilled") { |
| 53 | + return nativeResult.value as any |
| 54 | + } |
| 55 | + // Native failed — fall through to bridge result or throw |
| 56 | + if (bridgeResult.status === "fulfilled") { |
| 57 | + return bridgeResult.value as any |
| 58 | + } |
| 59 | + throw nativeResult.reason |
| 60 | + } catch (e) { |
| 61 | + // Shadow mode should not break the user — fall through to bridge |
| 62 | + return Bridge.call(method, params) |
| 63 | + } |
| 64 | + } |
| 65 | + return native(params) as any |
| 66 | + } |
| 67 | + |
| 68 | + if (process.env.ALTIMATE_NATIVE_ONLY === "1") { |
| 69 | + throw new Error( |
| 70 | + `No native handler for ${String(method)} (ALTIMATE_NATIVE_ONLY=1)`, |
| 71 | + ) |
| 72 | + } |
| 73 | + |
| 74 | + return Bridge.call(method, params) |
| 75 | +} |
| 76 | + |
| 77 | +/** Check if a native handler is registered for a method. */ |
| 78 | +export function hasNativeHandler(method: string): boolean { |
| 79 | + return nativeHandlers.has(method) |
| 80 | +} |
| 81 | + |
| 82 | +/** List all methods that have native handlers registered. */ |
| 83 | +export function listNativeMethods(): string[] { |
| 84 | + return Array.from(nativeHandlers.keys()) |
| 85 | +} |
0 commit comments