7
import {
8
clearApolloIdentityForAnalytics,
9
emitPostHogAlias,
10
+ ensureAnalyticsIdentity,
11
getCurrentCwdSessionId,
12
linkApolloIdentityForAnalytics,
13
readApiBaseUrl,
775
}).pipe(Effect.provide(makePlatformLayer(home)));
776
});
777
777
- it.effect('never re-aliases one install onto a second Apollo user', () => {
778
+ it.effect('rotates to a fresh install_id when a second Apollo user links', () => {
779
const home = tempy.temporaryDirectory();
780
const scriptPath = `${home}/composio.ts`;
781
enableTelemetry();
783
784
return Effect.gen(function* () {
785
const fs = yield* FileSystem.FileSystem;
786
+ const path = yield* Path.Path;
787
yield* fs.writeFileString(scriptPath, '');
788
789
yield* linkApolloIdentityForAnalytics('om_first_user');
790
expect(childProcessMocks.spawn).toHaveBeenCalledTimes(1);
791
+ const firstArgs = childProcessMocks.spawn.mock.calls[0]![1] as string[];
792
+ const firstPayload = decodeWorkerPayload<{ installId: string }>(firstArgs[2]!);
793
790
- // Logout, then log in as a different user on the same device.
794
+ // Logout, then log in as a different user on the same device: the
795
+ // second user is aliased to a fresh install_id, never the one already
796
+ // merged into the first user's PostHog person.
797
yield* clearApolloIdentityForAnalytics;
798
yield* linkApolloIdentityForAnalytics('om_second_user');
799
+ expect(childProcessMocks.spawn).toHaveBeenCalledTimes(2);
800
+ const secondArgs = childProcessMocks.spawn.mock.calls[1]![1] as string[];
801
+ const secondPayload = decodeWorkerPayload<{
802
+ event: string;
803
+ distinctId: string;
804
+ installId: string;
805
+ properties: { alias: string };
806
+ }>(secondArgs[2]!);
807
+ expect(secondPayload.event).toBe('$create_alias');
808
+ expect(secondPayload.distinctId).toBe('om_second_user');
809
+ expect(secondPayload.installId).not.toBe(firstPayload.installId);
810
+ expect(secondPayload.properties.alias).toBe(secondPayload.installId);
811
+
812
+ const persisted = JSON.parse(
813
+ yield* fs.readFileString(path.join(home, '.composio', 'analytics.json'), 'utf8')
814
+ ) as { install_id: string; apollo_user_id: string; aliased_apollo_user_id: string };
815
+ expect(persisted.install_id).toBe(secondPayload.installId);
816
+ expect(persisted.apollo_user_id).toBe('om_second_user');
817
+ expect(persisted.aliased_apollo_user_id).toBe('om_second_user');
818
+ }).pipe(Effect.provide(makePlatformLayer(home)));
819
+ });
820
+
821
+ it.effect('trusts the persisted identity when the api key lives in the OS keyring', () => {
822
+ const home = tempy.temporaryDirectory();
823
+ const scriptPath = `${home}/composio.ts`;
824
+ // No env key and no plaintext api_key on disk -> no fingerprint.
825
+ enableTelemetry('');
826
+ process.argv[1] = scriptPath;
827
+
828
+ return Effect.gen(function* () {
829
+ const fs = yield* FileSystem.FileSystem;
830
+ const path = yield* Path.Path;
831
+ yield* fs.writeFileString(scriptPath, '');
832
+ const composioDir = path.join(home, '.composio');
833
+ yield* fs.makeDirectory(composioDir, { recursive: true });
834
+ yield* fs.writeFileString(
835
+ path.join(composioDir, 'config.json'),
836
+ JSON.stringify({ security: 'keychain-subprocess' })
837
+ );
838
+ yield* fs.writeFileString(
839
+ path.join(composioDir, USER_CONFIG_FILE_NAME),
840
+ JSON.stringify({ base_url: 'https://backend.example.test', org_id: 'org_keychain' })
841
+ );
842
+ yield* fs.writeFileString(
843
+ path.join(composioDir, 'analytics.json'),
844
+ JSON.stringify({
845
+ install_id: 'install_keychain',
846
+ apollo_user_id: 'om_keychain_user',
847
+ aliased_apollo_user_id: 'om_keychain_user',
848
+ api_key_fingerprint: 'abc.5',
849
+ })
850
+ );
851
+
852
+ yield* trackCliEventEffect({ name: 'producer_event' });
853
+
854
+ const args = childProcessMocks.spawn.mock.calls[0]![1] as string[];
855
+ const payload = decodeWorkerPayload<{ distinctId: string }>(args[2]!);
856
+ expect(payload.distinctId).toBe('om_keychain_user');
857
+ }).pipe(Effect.provide(makePlatformLayer(home)));
858
+ });
859
+
860
+ it.effect('does not trust a fingerprint-less identity outside keychain modes', () => {
861
+ const home = tempy.temporaryDirectory();
862
+ const scriptPath = `${home}/composio.ts`;
863
+ enableTelemetry('');
864
+ process.argv[1] = scriptPath;
865
+
866
+ return Effect.gen(function* () {
867
+ const fs = yield* FileSystem.FileSystem;
868
+ const path = yield* Path.Path;
869
+ yield* fs.writeFileString(scriptPath, '');
870
+ const composioDir = path.join(home, '.composio');
871
+ yield* fs.makeDirectory(composioDir, { recursive: true });
872
+ yield* fs.writeFileString(
873
+ path.join(composioDir, 'config.json'),
874
+ JSON.stringify({ security: 'json' })
875
+ );
876
+ yield* fs.writeFileString(
877
+ path.join(composioDir, USER_CONFIG_FILE_NAME),
878
+ JSON.stringify({ org_id: 'org_json' })
879
+ );
880
+ yield* fs.writeFileString(
881
+ path.join(composioDir, 'analytics.json'),
882
+ JSON.stringify({
883
+ install_id: 'install_json',
884
+ apollo_user_id: 'om_json_user',
885
+ aliased_apollo_user_id: 'om_json_user',
886
+ api_key_fingerprint: 'abc.5',
887
+ })
888
+ );
889
+
890
+ yield* trackCliEventEffect({ name: 'producer_event' });
891
+
892
+ const args = childProcessMocks.spawn.mock.calls[0]![1] as string[];
893
+ const payload = decodeWorkerPayload<{ distinctId: string }>(args[2]!);
894
+ expect(payload.distinctId).toBe('anon_install_json');
895
+ }).pipe(Effect.provide(makePlatformLayer(home)));
896
+ });
897
+
898
+ it.effect('redacts secret-shaped tokens from event properties', () => {
899
+ const home = tempy.temporaryDirectory();
900
+ const scriptPath = `${home}/composio.ts`;
901
+ enableTelemetry();
902
+ process.argv[1] = scriptPath;
903
+
904
+ return Effect.gen(function* () {
905
+ const fs = yield* FileSystem.FileSystem;
906
+ yield* fs.writeFileString(scriptPath, '');
907
+
908
+ yield* trackCliEventEffect({
909
+ name: CLI_ANALYTICS_EVENTS.CLI_COMMAND_FAILED,
910
+ properties: {
911
+ error_message: 'Auth failed for uak_AbC123xyz_secret via Bearer eyJhbGciOi.Jt0ken',
912
+ nested: { tokens: ['ghp_ABCdef1234567890'] },
913
+ duration_ms: 42,
914
+ },
915
+ });
916
+
917
+ const args = childProcessMocks.spawn.mock.calls[0]![1] as string[];
918
+ const payload = decodeWorkerPayload<{
919
+ properties: {
920
+ error_message: string;
921
+ nested: { tokens: string[] };
922
+ duration_ms: number;
923
+ };
924
+ }>(args[2]!);
925
+ expect(payload.properties.error_message).toBe('Auth failed for [redacted] via [redacted]');
926
+ expect(payload.properties.nested.tokens).toEqual(['[redacted]']);
927
+ expect(payload.properties.duration_ms).toBe(42);
928
+ }).pipe(Effect.provide(makePlatformLayer(home)));
929
+ });
930
+
931
+ it.effect('bootstrap stitch links a missing identity through session info', () => {
932
+ const home = tempy.temporaryDirectory();
933
+ const scriptPath = `${home}/composio.ts`;
934
+ enableTelemetry('uak_stitch');
935
+ process.argv[1] = scriptPath;
936
+ const fetchSessionInfo = vi.fn(() => Effect.succeed({ org_member: { id: 'om_stitched' } }));
937
+
938
+ return Effect.gen(function* () {
939
+ const fs = yield* FileSystem.FileSystem;
940
+ yield* fs.writeFileString(scriptPath, '');
941
+
942
+ yield* ensureAnalyticsIdentity({
943
+ apiKey: 'uak_stitch',
944
+ baseURL: 'https://backend.example.test',
945
+ fetchSessionInfo,
946
+ });
947
+
948
+ expect(fetchSessionInfo).toHaveBeenCalledTimes(1);
949
expect(childProcessMocks.spawn).toHaveBeenCalledTimes(1);
950
+ const args = childProcessMocks.spawn.mock.calls[0]![1] as string[];
951
+ const payload = decodeWorkerPayload<{ event: string; distinctId: string }>(args[2]!);
952
+ expect(payload.event).toBe('$create_alias');
953
+ expect(payload.distinctId).toBe('om_stitched');
954
+
955
+ // Once linked under this credential, later runs make no network attempt.
956
+ yield* ensureAnalyticsIdentity({
957
+ apiKey: 'uak_stitch',
958
+ baseURL: 'https://backend.example.test',
959
+ fetchSessionInfo,
960
+ });
961
+ expect(fetchSessionInfo).toHaveBeenCalledTimes(1);
962
+ }).pipe(Effect.provide(makePlatformLayer(home)));
963
+ });
964
+
965
+ it.effect('bootstrap stitch attempts at most once per day after a failure', () => {
966
+ const home = tempy.temporaryDirectory();
967
+ const scriptPath = `${home}/composio.ts`;
968
+ enableTelemetry('uak_stitch');
969
+ process.argv[1] = scriptPath;
970
+ const fetchSessionInfo = vi.fn(() => Effect.fail(new Error('offline')));
971
+
972
+ return Effect.gen(function* () {
973
+ const fs = yield* FileSystem.FileSystem;
974
+ const path = yield* Path.Path;
975
+ yield* fs.writeFileString(scriptPath, '');
976
+
977
+ yield* ensureAnalyticsIdentity({
978
+ apiKey: 'uak_stitch',
979
+ baseURL: 'https://backend.example.test',
980
+ fetchSessionInfo,
981
+ });
982
+ expect(fetchSessionInfo).toHaveBeenCalledTimes(1);
983
+
984
+ const persisted = JSON.parse(
985
+ yield* fs.readFileString(path.join(home, '.composio', 'analytics.json'), 'utf8')
986
+ ) as { stitch_attempted_at?: string };
987
+ expect(typeof persisted.stitch_attempted_at).toBe('string');
988
+
989
+ // Identity is still unlinked, but the attempt is throttled for 24h.
990
+ yield* ensureAnalyticsIdentity({
991
+ apiKey: 'uak_stitch',
992
+ baseURL: 'https://backend.example.test',
993
+ fetchSessionInfo,
994
+ });
995
+ expect(fetchSessionInfo).toHaveBeenCalledTimes(1);
996
}).pipe(Effect.provide(makePlatformLayer(home)));
997
});
998
});