sessionIdCompat.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * Session ID tag translation helpers for the CCR v2 compat layer.
  3. *
  4. * Lives in its own file (rather than workSecret.ts) so that sessionHandle.ts
  5. * and replBridgeTransport.ts (bridge.mjs entry points) can import from
  6. * workSecret.ts without pulling in these retag functions.
  7. *
  8. * The isCseShimEnabled kill switch is injected via setCseShimGate() to avoid
  9. * a static import of bridgeEnabled.ts → growthbook.ts → config.ts — all
  10. * banned from the sdk.mjs bundle (scripts/build-agent-sdk.sh). Callers that
  11. * already import bridgeEnabled.ts register the gate; the SDK path never does,
  12. * so the shim defaults to active (matching isCseShimEnabled()'s own default).
  13. */
  14. let _isCseShimEnabled: (() => boolean) | undefined
  15. /**
  16. * Register the GrowthBook gate for the cse_ shim. Called from bridge
  17. * init code that already imports bridgeEnabled.ts.
  18. */
  19. export function setCseShimGate(gate: () => boolean): void {
  20. _isCseShimEnabled = gate
  21. }
  22. /**
  23. * Re-tag a `cse_*` session ID to `session_*` for use with the v1 compat API.
  24. *
  25. * Worker endpoints (/v1/code/sessions/{id}/worker/*) want `cse_*`; that's
  26. * what the work poll delivers. Client-facing compat endpoints
  27. * (/v1/sessions/{id}, /v1/sessions/{id}/archive, /v1/sessions/{id}/events)
  28. * want `session_*` — compat/convert.go:27 validates TagSession. Same UUID,
  29. * different costume. No-op for IDs that aren't `cse_*`.
  30. *
  31. * bridgeMain holds one sessionId variable for both worker registration and
  32. * session-management calls. It arrives as `cse_*` from the work poll under
  33. * the compat gate, so archiveSession/fetchSessionTitle need this re-tag.
  34. */
  35. export function toCompatSessionId(id: string): string {
  36. if (!id.startsWith('cse_')) return id
  37. if (_isCseShimEnabled && !_isCseShimEnabled()) return id
  38. return 'session_' + id.slice('cse_'.length)
  39. }
  40. /**
  41. * Re-tag a `session_*` session ID to `cse_*` for infrastructure-layer calls.
  42. *
  43. * Inverse of toCompatSessionId. POST /v1/environments/{id}/bridge/reconnect
  44. * lives below the compat layer: once ccr_v2_compat_enabled is on server-side,
  45. * it looks sessions up by their infra tag (`cse_*`). createBridgeSession still
  46. * returns `session_*` (compat/convert.go:41) and that's what bridge-pointer
  47. * stores — so perpetual reconnect passes the wrong costume and gets "Session
  48. * not found" back. Same UUID, wrong tag. No-op for IDs that aren't `session_*`.
  49. */
  50. export function toInfraSessionId(id: string): string {
  51. if (!id.startsWith('session_')) return id
  52. return 'cse_' + id.slice('session_'.length)
  53. }