Skip to content

Commit d99de38

Browse files
committed
fix(enum): add invariant check to prevent enum drift between OrchestrationStatus and protobuf
1 parent 06625a8 commit d99de38

2 files changed

Lines changed: 22 additions & 12 deletions

File tree

packages/durabletask-js/src/client/logs.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
* Event ID ranges:
1111
* - 40–46: Client operations (from .NET)
12-
* - 47–55: JS-specific client operations
12+
* - 100–199: JS-specific client operations
1313
*/
1414

1515
import { Logger } from "../types/logger.type";
@@ -29,16 +29,16 @@ const CATEGORY_CLIENT = "Microsoft.DurableTask.Client";
2929
/** @internal */ export const EVENT_PURGING_INSTANCE_METADATA = 45;
3030
/** @internal */ export const EVENT_PURGING_INSTANCES = 46;
3131

32-
// ── JS-specific Event IDs ────────────────────────────────────────────────────
32+
// ── JS-specific Event IDs (start at 100 to leave buffer for .NET additions) ──
3333

34-
/** @internal */ export const EVENT_RAISING_EVENT = 47;
35-
/** @internal */ export const EVENT_SUSPENDING_INSTANCE = 48;
36-
/** @internal */ export const EVENT_RESUMING_INSTANCE = 49;
37-
/** @internal */ export const EVENT_REWINDING_INSTANCE = 50;
38-
/** @internal */ export const EVENT_RESTARTING_INSTANCE = 51;
39-
/** @internal */ export const EVENT_INSTANCE_COMPLETED = 52;
40-
/** @internal */ export const EVENT_INSTANCE_FAILED = 53;
41-
/** @internal */ export const EVENT_INSTANCE_TERMINATED = 54;
34+
/** @internal */ export const EVENT_RAISING_EVENT = 100;
35+
/** @internal */ export const EVENT_SUSPENDING_INSTANCE = 101;
36+
/** @internal */ export const EVENT_RESUMING_INSTANCE = 102;
37+
/** @internal */ export const EVENT_REWINDING_INSTANCE = 103;
38+
/** @internal */ export const EVENT_RESTARTING_INSTANCE = 104;
39+
/** @internal */ export const EVENT_INSTANCE_COMPLETED = 105;
40+
/** @internal */ export const EVENT_INSTANCE_FAILED = 106;
41+
/** @internal */ export const EVENT_INSTANCE_TERMINATED = 107;
4242

4343
// ═══════════════════════════════════════════════════════════════════════════════
4444
// Client Operation Logs (Event IDs 40–46, matching .NET)
@@ -127,7 +127,7 @@ export function purgingInstances(
127127
}
128128

129129
// ═══════════════════════════════════════════════════════════════════════════════
130-
// JS-specific Client Logs (Event IDs 47–54)
130+
// JS-specific Client Logs (Event IDs 100+)
131131
// ═══════════════════════════════════════════════════════════════════════════════
132132

133133
export function raisingEvent(logger: Logger, instanceId: string, eventName: string): void {

packages/durabletask-js/src/orchestration/enum/orchestration-status.enum.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,19 @@ export enum OrchestrationStatus {
3636

3737
// Initialize the reverse maps now that both enums are defined.
3838
// Both enums share the same numeric values; cast through number to satisfy TypeScript.
39-
for (const [, value] of Object.entries(OrchestrationStatus)) {
39+
// An invariant check guards against silent drift between the two enums.
40+
for (const [name, value] of Object.entries(OrchestrationStatus)) {
4041
if (typeof value === "number") {
4142
const numValue = value as number;
43+
const expectedProtoKey = `ORCHESTRATION_STATUS_${name}`;
44+
// Verify the proto enum has this key with the same numeric value (plain object, no reverse mapping)
45+
const protoValue = (pb.OrchestrationStatus as unknown as Record<string, number>)[expectedProtoKey];
46+
if (protoValue !== numValue) {
47+
throw new Error(
48+
`Enum drift detected: OrchestrationStatus.${name} (${numValue}) does not match ` +
49+
`pb.OrchestrationStatus.${expectedProtoKey} (${protoValue}).`,
50+
);
51+
}
4252
protoToClient.set(numValue as pb.OrchestrationStatus, numValue as OrchestrationStatus);
4353
clientToProto.set(numValue as OrchestrationStatus, numValue as pb.OrchestrationStatus);
4454
}

0 commit comments

Comments
 (0)