Skip to content

Commit e9b6d93

Browse files
anandgupta42claude
andcommitted
feat: [Phase 0] add Dispatcher abstraction layer for Python bridge migration
Introduces a Strangler Fig pattern for incrementally replacing the Python altimate-engine bridge with native TypeScript implementations. - Create native/dispatcher.ts with typed register() and call() functions - Create native/index.ts barrel export - Update all 66 tool files: Bridge.call() -> Dispatcher.call() - Add ALTIMATE_NATIVE_ONLY=1 feature flag (CI gate) - Add ALTIMATE_SHADOW_MODE=1 for parity testing (runs both paths, logs mismatches) - Zero behavior change -- all calls fall through to Python bridge - Update altimate/index.ts to export Dispatcher Closes #215 Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1 parent 1ca92bc commit e9b6d93

69 files changed

Lines changed: 230 additions & 141 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎packages/opencode/src/altimate/index.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
// Barrel export for all Altimate custom code
22

3-
// Bridge
3+
// Bridge (legacy — being replaced by native dispatcher)
44
export { Bridge } from "./bridge/client"
55
export { ensureEngine, enginePythonPath } from "./bridge/engine"
66
export * from "./bridge/protocol"
77

8+
// Native dispatcher (new — Strangler Fig migration layer)
9+
export { Dispatcher } from "./native"
10+
811
// Telemetry
912
export { Telemetry } from "./telemetry"
1013

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * as Dispatcher from "./dispatcher"

‎packages/opencode/src/altimate/tools/altimate-core-check.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import z from "zod"
22
import { Tool } from "../../tool/tool"
3-
import { Bridge } from "../bridge/client"
3+
import { Dispatcher } from "../native"
44

55
export const AltimateCoreCheckTool = Tool.define("altimate_core_check", {
66
description:
@@ -12,7 +12,7 @@ export const AltimateCoreCheckTool = Tool.define("altimate_core_check", {
1212
}),
1313
async execute(args, ctx) {
1414
try {
15-
const result = await Bridge.call("altimate_core.check", {
15+
const result = await Dispatcher.call("altimate_core.check", {
1616
sql: args.sql,
1717
schema_path: args.schema_path ?? "",
1818
schema_context: args.schema_context,

‎packages/opencode/src/altimate/tools/altimate-core-classify-pii.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import z from "zod"
22
import { Tool } from "../../tool/tool"
3-
import { Bridge } from "../bridge/client"
3+
import { Dispatcher } from "../native"
44

55
export const AltimateCoreClassifyPiiTool = Tool.define("altimate_core_classify_pii", {
66
description:
@@ -11,7 +11,7 @@ export const AltimateCoreClassifyPiiTool = Tool.define("altimate_core_classify_p
1111
}),
1212
async execute(args, ctx) {
1313
try {
14-
const result = await Bridge.call("altimate_core.classify_pii", {
14+
const result = await Dispatcher.call("altimate_core.classify_pii", {
1515
schema_path: args.schema_path ?? "",
1616
schema_context: args.schema_context,
1717
})

‎packages/opencode/src/altimate/tools/altimate-core-column-lineage.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import z from "zod"
22
import { Tool } from "../../tool/tool"
3-
import { Bridge } from "../bridge/client"
3+
import { Dispatcher } from "../native"
44

55
export const AltimateCoreColumnLineageTool = Tool.define("altimate_core_column_lineage", {
66
description:
@@ -13,7 +13,7 @@ export const AltimateCoreColumnLineageTool = Tool.define("altimate_core_column_l
1313
}),
1414
async execute(args, ctx) {
1515
try {
16-
const result = await Bridge.call("altimate_core.column_lineage", {
16+
const result = await Dispatcher.call("altimate_core.column_lineage", {
1717
sql: args.sql,
1818
dialect: args.dialect ?? "",
1919
schema_path: args.schema_path ?? "",

‎packages/opencode/src/altimate/tools/altimate-core-compare.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import z from "zod"
22
import { Tool } from "../../tool/tool"
3-
import { Bridge } from "../bridge/client"
3+
import { Dispatcher } from "../native"
44

55
export const AltimateCoreCompareTool = Tool.define("altimate_core_compare", {
66
description:
@@ -12,7 +12,7 @@ export const AltimateCoreCompareTool = Tool.define("altimate_core_compare", {
1212
}),
1313
async execute(args, ctx) {
1414
try {
15-
const result = await Bridge.call("altimate_core.compare", {
15+
const result = await Dispatcher.call("altimate_core.compare", {
1616
left_sql: args.left_sql,
1717
right_sql: args.right_sql,
1818
dialect: args.dialect ?? "",

‎packages/opencode/src/altimate/tools/altimate-core-complete.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import z from "zod"
22
import { Tool } from "../../tool/tool"
3-
import { Bridge } from "../bridge/client"
3+
import { Dispatcher } from "../native"
44

55
export const AltimateCoreCompleteTool = Tool.define("altimate_core_complete", {
66
description:
@@ -13,7 +13,7 @@ export const AltimateCoreCompleteTool = Tool.define("altimate_core_complete", {
1313
}),
1414
async execute(args, ctx) {
1515
try {
16-
const result = await Bridge.call("altimate_core.complete", {
16+
const result = await Dispatcher.call("altimate_core.complete", {
1717
sql: args.sql,
1818
cursor_pos: args.cursor_pos,
1919
schema_path: args.schema_path ?? "",

‎packages/opencode/src/altimate/tools/altimate-core-correct.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import z from "zod"
22
import { Tool } from "../../tool/tool"
3-
import { Bridge } from "../bridge/client"
3+
import { Dispatcher } from "../native"
44

55
export const AltimateCoreCorrectTool = Tool.define("altimate_core_correct", {
66
description:
@@ -12,7 +12,7 @@ export const AltimateCoreCorrectTool = Tool.define("altimate_core_correct", {
1212
}),
1313
async execute(args, ctx) {
1414
try {
15-
const result = await Bridge.call("altimate_core.correct", {
15+
const result = await Dispatcher.call("altimate_core.correct", {
1616
sql: args.sql,
1717
schema_path: args.schema_path ?? "",
1818
schema_context: args.schema_context,

‎packages/opencode/src/altimate/tools/altimate-core-equivalence.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import z from "zod"
22
import { Tool } from "../../tool/tool"
3-
import { Bridge } from "../bridge/client"
3+
import { Dispatcher } from "../native"
44

55
export const AltimateCoreEquivalenceTool = Tool.define("altimate_core_equivalence", {
66
description:
@@ -13,7 +13,7 @@ export const AltimateCoreEquivalenceTool = Tool.define("altimate_core_equivalenc
1313
}),
1414
async execute(args, ctx) {
1515
try {
16-
const result = await Bridge.call("altimate_core.equivalence", {
16+
const result = await Dispatcher.call("altimate_core.equivalence", {
1717
sql1: args.sql1,
1818
sql2: args.sql2,
1919
schema_path: args.schema_path ?? "",

0 commit comments

Comments
 (0)