Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { describe, expect, it } from '@effect/vitest'; |
| 2 | import { Array as Arr, ConfigProvider, Effect, Option, pipe } from 'effect'; |
| 3 | import { TELEMETRY_CONFIG, type AgentSessionEnvironment } from 'src/config'; |
| 4 | import { |
| 5 | AGENT_SESSION_NAMESPACE, |
| 6 | deriveAgentSessionId, |
| 7 | resolveAgentSession, |
| 8 | selectHostSession, |
| 9 | type HostSession, |
| 10 | } from 'src/services/agent-session'; |
| 11 | |
| 12 | // Made-up host session IDs and the UUID v5 each one derives to. |
| 13 | const CLAUDE_SESSION = '00000000-0000-4000-8000-000000000001'; |
| 14 | const CLAUDE_AGENT_SESSION_ID = 'df095fd9-410b-5ec9-b678-4ed04b5ab2dd'; |
| 15 | const CODEX_SESSION = '00000000-0000-7000-8000-000000000002'; |
| 16 | const CODEX_AGENT_SESSION_ID = '9d27e7e4-b2ed-522c-bed8-595444e853a7'; |
| 17 | |
| 18 | describe('deriveAgentSessionId', () => { |
| 19 | it('[Given] the namespace name [Then] it is the namespace other systems derive under', () => { |
| 20 | expect(AGENT_SESSION_NAMESPACE).toBe('377a1a01-b6e5-5f6e-ae1c-a874a3316d89'); |
| 21 | }); |
| 22 | |
| 23 | it.each([ |
| 24 | { host: 'claude', hostSessionId: CLAUDE_SESSION, expected: CLAUDE_AGENT_SESSION_ID }, |
| 25 | { host: 'codex', hostSessionId: CODEX_SESSION, expected: CODEX_AGENT_SESSION_ID }, |
| 26 | ] as const)( |
| 27 | '[Given] host $host and session $hostSessionId [Then] the ID is $expected', |
| 28 | ({ host, hostSessionId, expected }) => { |
| 29 | expect(deriveAgentSessionId({ host, hostSessionId })).toBe(expected); |
| 30 | } |
| 31 | ); |
| 32 | |
| 33 | it('[Given] the same session ID in upper case [Then] the ID differs', () => { |
| 34 | const hostSessionId = 'abcdef00-0000-4000-8000-000000000001'; |
| 35 | expect( |
| 36 | deriveAgentSessionId({ host: 'claude', hostSessionId: hostSessionId.toUpperCase() }) |
| 37 | ).not.toBe(deriveAgentSessionId({ host: 'claude', hostSessionId })); |
| 38 | }); |
| 39 | }); |
| 40 | |
| 41 | const NO_VARIABLES: AgentSessionEnvironment = { |
| 42 | hookHost: undefined, |
| 43 | hookSessionId: undefined, |
| 44 | claudeCodeSessionId: undefined, |
| 45 | codexSessionId: undefined, |
| 46 | codexThreadId: undefined, |
| 47 | }; |
| 48 | |
| 49 | type SelectionCase = { |
| 50 | readonly name: string; |
| 51 | readonly variables: Partial<AgentSessionEnvironment>; |
| 52 | readonly expected: HostSession | undefined; |
| 53 | }; |
| 54 | |
| 55 | const SELECTION_CASES: ReadonlyArray<SelectionCase> = [ |
| 56 | { |
| 57 | name: 'a hook export for claude and a different CLAUDE_CODE_SESSION_ID: the export wins', |
| 58 | variables: { hookHost: 'claude', hookSessionId: 'hook-chat', claudeCodeSessionId: 'own-chat' }, |
| 59 | expected: { host: 'claude', hostSessionId: 'hook-chat' }, |
| 60 | }, |
| 61 | { |
| 62 | name: 'a hook export for codex and a different CLAUDE_CODE_SESSION_ID: the export wins', |
| 63 | variables: { hookHost: 'codex', hookSessionId: 'hook-chat', claudeCodeSessionId: 'own-chat' }, |
| 64 | expected: { host: 'codex', hostSessionId: 'hook-chat' }, |
| 65 | }, |
| 66 | { |
| 67 | name: 'a hook export for claude inside Codex: the parent chat is reported', |
| 68 | variables: { hookHost: 'claude', hookSessionId: 'hook-chat', codexSessionId: 'codex-chat' }, |
| 69 | expected: { host: 'claude', hostSessionId: 'hook-chat' }, |
| 70 | }, |
| 71 | { |
| 72 | name: 'a hook export for an unknown host: the export is ignored', |
| 73 | variables: { hookHost: 'pi', hookSessionId: 'hook-chat', claudeCodeSessionId: 'own-chat' }, |
| 74 | expected: { host: 'claude', hostSessionId: 'own-chat' }, |
| 75 | }, |
| 76 | { |
| 77 | name: 'a hook host without a hook session ID: treated as no export', |
| 78 | variables: { hookHost: 'codex', claudeCodeSessionId: 'own-chat' }, |
| 79 | expected: { host: 'claude', hostSessionId: 'own-chat' }, |
| 80 | }, |
| 81 | { |
| 82 | name: 'a hook session ID without a hook host: treated as no export', |
| 83 | variables: { hookSessionId: 'hook-chat', codexSessionId: 'codex-chat' }, |
| 84 | expected: { host: 'codex', hostSessionId: 'codex-chat' }, |
| 85 | }, |
| 86 | { |
| 87 | name: 'only CLAUDE_CODE_SESSION_ID: host claude', |
| 88 | variables: { claudeCodeSessionId: 'own-chat' }, |
| 89 | expected: { host: 'claude', hostSessionId: 'own-chat' }, |
| 90 | }, |
| 91 | { |
| 92 | name: 'only CODEX_SESSION_ID: host codex', |
| 93 | variables: { codexSessionId: 'codex-chat' }, |
| 94 | expected: { host: 'codex', hostSessionId: 'codex-chat' }, |
| 95 | }, |
| 96 | { |
| 97 | name: 'CODEX_SESSION_ID and a different CODEX_THREAD_ID: the session ID is used', |
| 98 | variables: { codexSessionId: 'codex-chat', codexThreadId: 'codex-thread' }, |
| 99 | expected: { host: 'codex', hostSessionId: 'codex-chat' }, |
| 100 | }, |
| 101 | { |
| 102 | name: 'only CODEX_THREAD_ID: host codex with the thread ID', |
| 103 | variables: { codexThreadId: 'codex-thread' }, |
| 104 | expected: { host: 'codex', hostSessionId: 'codex-thread' }, |
| 105 | }, |
| 106 | { |
| 107 | name: 'CLAUDE_CODE_SESSION_ID and CODEX_SESSION_ID with no export: none', |
| 108 | variables: { claudeCodeSessionId: 'own-chat', codexSessionId: 'codex-chat' }, |
| 109 | expected: undefined, |
| 110 | }, |
| 111 | { |
| 112 | name: 'CLAUDE_CODE_SESSION_ID and CODEX_THREAD_ID with no export: none', |
| 113 | variables: { claudeCodeSessionId: 'own-chat', codexThreadId: 'codex-thread' }, |
| 114 | expected: undefined, |
| 115 | }, |
| 116 | { name: 'no variable: none', variables: {}, expected: undefined }, |
| 117 | ]; |
| 118 | |
| 119 | describe('selectHostSession', () => { |
| 120 | it.each(SELECTION_CASES)('[Given] $name', ({ variables, expected }) => { |
| 121 | expect( |
| 122 | Option.getOrUndefined(selectHostSession({ ...NO_VARIABLES, ...variables })) |
| 123 | ).toStrictEqual(expected); |
| 124 | }); |
| 125 | }); |
| 126 | |
| 127 | // Each record is the whole environment the resolver sees, so the shell that runs the suite |
| 128 | // (its `NODE_ENV=test`, `CI`, and agent-host session variables) cannot change a result. |
| 129 | const isolatedEnvironment = (env: Record<string, string>) => ConfigProvider.fromEnvRecord(env); |
| 130 | |
| 131 | const resolveWith = (env: Record<string, string>) => |
| 132 | resolveAgentSession.pipe( |
| 133 | Effect.provideService(ConfigProvider.ConfigProvider, isolatedEnvironment(env)), |
| 134 | Effect.map(Option.getOrUndefined) |
| 135 | ); |
| 136 | |
| 137 | const TELEMETRY_ON = { CI: 'false', NODE_ENV: 'production' }; |
| 138 | |
| 139 | type SessionSource = { |
| 140 | readonly source: string; |
| 141 | readonly variables: Record<string, string>; |
| 142 | }; |
| 143 | |
| 144 | // Both ways a session resolves. Each resolves one in the tests below while telemetry is on. |
| 145 | const SESSION_SOURCES: ReadonlyArray<SessionSource> = [ |
| 146 | { source: 'a host variable', variables: { CLAUDE_CODE_SESSION_ID: CLAUDE_SESSION } }, |
| 147 | { |
| 148 | source: 'a hook export', |
| 149 | variables: { COMPOSIO_AGENT_HOST: 'claude', COMPOSIO_AGENT_SESSION_ID: CLAUDE_SESSION }, |
| 150 | }, |
| 151 | ]; |
| 152 | |
| 153 | const TELEMETRY_DISABLED_CASES = pipe( |
| 154 | [ |
| 155 | { name: 'COMPOSIO_CLI_TELEMETRY_DISABLED', value: '1' }, |
| 156 | { name: 'COMPOSIO_DISABLE_TELEMETRY', value: 'true' }, |
| 157 | { name: 'TELEMETRY_DISABLED', value: '1' }, |
| 158 | { name: 'CI', value: 'true' }, |
| 159 | { name: 'NODE_ENV', value: 'test' }, |
| 160 | ], |
| 161 | Arr.flatMap(telemetrySwitch => |
| 162 | pipe( |
| 163 | SESSION_SOURCES, |
| 164 | Arr.map(sessionSource => ({ ...telemetrySwitch, ...sessionSource })) |
| 165 | ) |
| 166 | ) |
| 167 | ); |
| 168 | |
| 169 | describe('resolveAgentSession', () => { |
| 170 | it.effect('[Given] CLAUDE_CODE_SESSION_ID [Then] it resolves claude and the derived ID', () => |
| 171 | Effect.gen(function* () { |
| 172 | const actual = yield* resolveWith({ |
| 173 | ...TELEMETRY_ON, |
| 174 | CLAUDE_CODE_SESSION_ID: CLAUDE_SESSION, |
| 175 | }); |
| 176 | expect(actual).toStrictEqual({ host: 'claude', id: CLAUDE_AGENT_SESSION_ID }); |
| 177 | }) |
| 178 | ); |
| 179 | |
| 180 | it.effect('[Given] CODEX_THREAD_ID alone [Then] it resolves codex and the derived ID', () => |
| 181 | Effect.gen(function* () { |
| 182 | const actual = yield* resolveWith({ ...TELEMETRY_ON, CODEX_THREAD_ID: CODEX_SESSION }); |
| 183 | expect(actual).toStrictEqual({ host: 'codex', id: CODEX_AGENT_SESSION_ID }); |
| 184 | }) |
| 185 | ); |
| 186 | |
| 187 | it.effect('[Given] a hook export inside another host [Then] it resolves the exported chat', () => |
| 188 | Effect.gen(function* () { |
| 189 | const actual = yield* resolveWith({ |
| 190 | ...TELEMETRY_ON, |
| 191 | COMPOSIO_AGENT_HOST: 'claude', |
| 192 | COMPOSIO_AGENT_SESSION_ID: CLAUDE_SESSION, |
| 193 | CODEX_SESSION_ID: CODEX_SESSION, |
| 194 | }); |
| 195 | expect(actual).toStrictEqual({ host: 'claude', id: CLAUDE_AGENT_SESSION_ID }); |
| 196 | }) |
| 197 | ); |
| 198 | |
| 199 | it.effect('[Given] variables from both hosts and no export [Then] it resolves none', () => |
| 200 | Effect.gen(function* () { |
| 201 | const actual = yield* resolveWith({ |
| 202 | ...TELEMETRY_ON, |
| 203 | CLAUDE_CODE_SESSION_ID: CLAUDE_SESSION, |
| 204 | CODEX_SESSION_ID: CODEX_SESSION, |
| 205 | }); |
| 206 | expect(actual).toBeUndefined(); |
| 207 | }) |
| 208 | ); |
| 209 | |
| 210 | it.effect('[Given] no agent session variable [Then] it resolves none', () => |
| 211 | Effect.gen(function* () { |
| 212 | expect(yield* resolveWith(TELEMETRY_ON)).toBeUndefined(); |
| 213 | }) |
| 214 | ); |
| 215 | |
| 216 | it.effect.each(TELEMETRY_DISABLED_CASES)( |
| 217 | '[Given] $source and telemetry disabled by $name=$value [Then] it resolves none', |
| 218 | ({ name, value, variables }) => |
| 219 | Effect.gen(function* () { |
| 220 | const actual = yield* resolveWith({ ...TELEMETRY_ON, ...variables, [name]: value }); |
| 221 | expect(actual).toBeUndefined(); |
| 222 | }) |
| 223 | ); |
| 224 | |
| 225 | it.effect('[Given] telemetry enabled and no PostHog key [Then] it still resolves', () => |
| 226 | Effect.gen(function* () { |
| 227 | const env = { ...TELEMETRY_ON, CLAUDE_CODE_SESSION_ID: CLAUDE_SESSION }; |
| 228 | expect(yield* TELEMETRY_CONFIG.POSTHOG_PROJECT_API_KEY.parse(isolatedEnvironment(env))).toBe( |
| 229 | '' |
| 230 | ); |
| 231 | expect(yield* resolveWith(env)).toStrictEqual({ |
| 232 | host: 'claude', |
| 233 | id: CLAUDE_AGENT_SESSION_ID, |
| 234 | }); |
| 235 | }) |
| 236 | ); |
| 237 | }); |